com.pivotal.gemfirexd.internal.client.net.NetConnection40 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snappydata-store-client Show documentation
Show all versions of snappydata-store-client Show documentation
SnappyData store based off Pivotal GemFireXD
/*=========================================================================
* Copyright (c) 2010-2014 Pivotal Software, Inc. All Rights Reserved.
* This product is protected by U.S. and international copyright
* and intellectual property laws. Pivotal products are covered by
* one or more patents listed at http://www.pivotal.io/patents.
*=========================================================================
*/
/*
Derby - Class com.pivotal.gemfirexd.internal.client.net.NetConnection40
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.pivotal.gemfirexd.internal.client.net;
import java.sql.Array;
import com.pivotal.gemfirexd.internal.client.am.SQLExceptionFactory;
import com.pivotal.gemfirexd.internal.client.am.SqlException;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLXML;
import java.sql.Struct;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Enumeration;
import java.util.concurrent.Executor;
import com.pivotal.gemfirexd.internal.client.ClientPooledConnection;
import com.pivotal.gemfirexd.internal.client.am.ClientMessageId;
import com.pivotal.gemfirexd.internal.client.am.FailedProperties40;
import com.pivotal.gemfirexd.internal.shared.common.reference.SQLState;
public class NetConnection40 extends com.pivotal.gemfirexd.internal.client.net.NetConnection {
/**
* Prepared statement that is used each time isValid() is called on this
* connection. The statement is created the first time isValid is called
* and closed when the connection is closed (by the close call).
*/
private PreparedStatement isValidStmt = null;
/*
*-------------------------------------------------------
* JDBC 4.0
*-------------------------------------------------------
*/
public NetConnection40(NetLogWriter netLogWriter,
String databaseName,
java.util.Properties properties) throws SqlException {
super(netLogWriter,databaseName,properties);
}
public NetConnection40(NetLogWriter netLogWriter,
com.pivotal.gemfirexd.internal.jdbc.ClientBaseDataSource dataSource,
String user,
String password) throws SqlException {
super(netLogWriter,dataSource,user,password);
}
public NetConnection40(NetLogWriter netLogWriter,
int driverManagerLoginTimeout,
String serverName,
int portNumber,
String databaseName,
java.util.Properties properties) throws SqlException{
super(netLogWriter,driverManagerLoginTimeout,serverName,portNumber,databaseName,properties);
}
public NetConnection40(NetLogWriter netLogWriter,
String user,
String password,
com.pivotal.gemfirexd.internal.jdbc.ClientBaseDataSource dataSource,
int rmId,
boolean isXAConn) throws SqlException{
super(netLogWriter,user,password,dataSource,rmId,isXAConn);
}
public NetConnection40(NetLogWriter netLogWriter,
String ipaddr,
int portNumber,
com.pivotal.gemfirexd.internal.jdbc.ClientBaseDataSource dataSource,
boolean isXAConn) throws SqlException{
super(netLogWriter,ipaddr,portNumber,dataSource,isXAConn);
}
/**
* The constructor for the NetConnection40 class which contains
* implementations of JDBC 4.0 specific methods in the java.sql.Connection
* interface. This constructor is called from the ClientPooledConnection object
* to enable the NetConnection to pass this
on to the associated
* prepared statement object thus enabling the prepared statement object
* to inturn raise the statement events to the ClientPooledConnection object.
*
* @param netLogWriter NetLogWriter object associated with this connection.
* @param user user id for this connection.
* @param password password for this connection.
* @param dataSource The DataSource object passed from the PooledConnection
* object from which this constructor was called.
* @param rmId The Resource manager ID for XA Connections
* @param isXAConn true if this is a XA connection.
* @param cpc The ClientPooledConnection object from which this
* NetConnection constructor was called. This is used
* to pass StatementEvents back to the pooledConnection
* object.
* @throws SqlException
*/
public NetConnection40(NetLogWriter netLogWriter,
String user,
String password,
com.pivotal.gemfirexd.internal.jdbc.ClientBaseDataSource dataSource,
int rmId,
boolean isXAConn,
ClientPooledConnection cpc) throws SqlException{
super(netLogWriter,user,password,dataSource,rmId,isXAConn,cpc);
}
public Array createArrayOf(String typeName, Object[] elements)
throws SQLException {
throw SQLExceptionFactory.notImplemented ("createArrayOf(String,Object[])");
}
public NClob createNClob() throws SQLException {
throw SQLExceptionFactory.notImplemented ("createNClob ()");
}
public SQLXML createSQLXML() throws SQLException {
throw SQLExceptionFactory.notImplemented ("createSQLXML ()");
}
public Struct createStruct(String typeName, Object[] attributes)
throws SQLException {
throw SQLExceptionFactory.notImplemented ("createStruct(String,Object[])");
}
/**
* Checks if the connection has not been closed and is still valid.
* The validity is checked by running a simple query against the
* database.
*
* The timeout specified by the caller is implemented as follows:
* On the server: uses the queryTimeout functionality to make the
* query time out on the server in case the server has problems or
* is highly loaded.
* On the client: uses a timeout on the socket to make sure that
* the client is not blocked forever in the cases where the server
* is "hanging" or not sending the reply.
*
* @param timeout The time in seconds to wait for the database
* operation used to validate the connection to complete. If the
* timeout period expires before the operation completes, this
* method returns false. A value of 0 indicates a timeout is not
* applied to the database operation.
* @return true if the connection is valid, false otherwise
* @exception SQLException if the parameter value is illegal or if a
* database error has occured
*/
public boolean isValid(int timeout) throws SQLException {
// Validate that the timeout has a legal value
if (timeout < 0) {
throw new SqlException(agent_.logWriter_,
new ClientMessageId(SQLState.INVALID_API_PARAMETER),
new Integer(timeout), "timeout",
"java.sql.Connection.isValid" ).getSQLException(
agent_ /* GemStoneAddition */);
}
// Check if the connection is closed
if (isClosed()) {
return false;
}
// Do a simple query against the database
synchronized(this) {
try {
// Save the current network timeout value
int oldTimeout = netAgent_.getTimeout();
// Set the required timeout value on the network connection
netAgent_.setTimeout(timeout);
// If this is the first time this method is called on this
// connection we prepare the query
if (isValidStmt == null) {
isValidStmt = prepareStatement("VALUES (1)");
}
// Set the query timeout
isValidStmt.setQueryTimeout(timeout);
// Run the query against the database
ResultSet rs = isValidStmt.executeQuery();
rs.close();
// Restore the previous timeout value
netAgent_.setTimeout(oldTimeout);
} catch(SQLException e) {
// If an SQL exception is thrown the connection is not valid,
// we ignore the exception and return false.
return false;
}
}
return true; // The connection is valid
}
/**
* Close the connection and release its resources.
* @exception SQLException if a database-access error occurs.
*/
synchronized public void close() throws SQLException {
// Release resources owned by the prepared statement used by isValid
if (isValidStmt != null) {
isValidStmt.close();
isValidStmt = null;
}
super.close();
}
/**
* setClientInfo
will always throw a
* SQLClientInfoException
since Derby does not support
* any properties.
*
* @param name a property key String
* @param value a property value String
* @exception SQLException always.
*/
public void setClientInfo(String name, String value)
throws SQLClientInfoException{
Properties p = FailedProperties40.makeProperties(name,value);
try { checkForClosedConnection(); }
catch (SqlException se) {
throw new SQLClientInfoException
(se.getMessage(), se.getSQLState(),
se.getErrorCode(),
new FailedProperties40(p).getProperties());
}
if (name == null && value == null) {
return;
}
setClientInfo(p);
}
/**
* setClientInfo
will throw a
* SQLClientInfoException
uless the properties
* paramenter is empty, since Derby does not support any
* properties. All the property keys in the
* properties
parameter are added to failedProperties
* of the exception thrown, with REASON_UNKNOWN_PROPERTY as the
* value.
*
* @param properties a Properties
object with the
* properties to set.
* @exception SQLClientInfoException unless the properties
* parameter is null or empty.
*/
public void setClientInfo(Properties properties)
throws SQLClientInfoException {
FailedProperties40 fp = new FailedProperties40(properties);
try { checkForClosedConnection(); }
catch (SqlException se) {
throw new SQLClientInfoException(se.getMessage(), se.getSQLState(),
se.getErrorCode(),
fp.getProperties());
}
if (properties == null || properties.isEmpty()) {
return;
}
SqlException se =
new SqlException(agent_.logWriter_,
new ClientMessageId
(SQLState.PROPERTY_UNSUPPORTED_CHANGE),
fp.getFirstKey(), fp.getFirstValue());
throw new SQLClientInfoException(se.getMessage(),
se.getSQLState(),
se.getErrorCode(),
fp.getProperties());
}
/**
* getClientInfo
always returns a
* null String
since Derby doesn't support
* ClientInfoProperties.
*
* @param name a String
value
* @return a null String
value
* @exception SQLException if the connection is closed.
*/
public String getClientInfo(String name)
throws SQLException{
try {
checkForClosedConnection();
return null;
}
catch (SqlException se) { throw se.getSQLException(
agent_ /* GemStoneAddition */); }
}
/**
* getClientInfo
always returns an empty
* Properties
object since Derby doesn't support
* ClientInfoProperties.
*
* @return an empty Properties
object.
* @exception SQLException if the connection is closed.
*/
public Properties getClientInfo()
throws SQLException{
try {
checkForClosedConnection();
return new Properties();
}
catch (SqlException se) { throw se.getSQLException(
agent_ /* GemStoneAddition */); }
}
/**
* Returns the type map for this connection.
*
* @return type map for this connection
* @exception SQLException if a database access error occurs
*/
public final Map> getTypeMap() throws SQLException {
// This method is already implemented with a non-generic
// signature in am/Connection. We could just use that method
// directly, but then we get a compiler warning (unchecked
// cast/conversion). Copy the map to avoid the compiler
// warning.
Map typeMap = super.getTypeMap();
if (typeMap == null) return null;
Map> genericTypeMap = new HashMap>();
for (Object key : typeMap.keySet()) {
genericTypeMap.put((String) key, (Class) typeMap.get(key));
}
return genericTypeMap;
}
/**
* Returns false unless interfaces
is implemented
*
* @param interfaces a Class defining an interface.
* @return true if this implements the interface or
* directly or indirectly wraps an object
* that does.
* @throws java.sql.SQLException if an error occurs while determining
* whether this is a wrapper for an object
* with the given interface.
*/
public boolean isWrapperFor(Class> interfaces) throws SQLException {
try {
checkForClosedConnection();
} catch (SqlException se) {
throw se.getSQLException(agent_ /* GemStoneAddition */);
}
return interfaces.isInstance(this);
}
/**
* Returns this
if this class implements the interface
*
* @param interfaces a Class defining an interface
* @return an object that implements the interface
* @throws java.sql.SQLException if no object if found that implements the
* interface
*/
public T unwrap(java.lang.Class interfaces)
throws SQLException {
try {
checkForClosedConnection();
return interfaces.cast(this);
} catch (ClassCastException cce) {
throw new SqlException(null,
new ClientMessageId(SQLState.UNABLE_TO_UNWRAP),
interfaces).getSQLException(agent_ /* GemStoneAddition */);
} catch (SqlException se) {
throw se.getSQLException(agent_ /* GemStoneAddition */);
}
}
public void setSchema(String schema) throws SQLException {
// TODO Auto-generated method stub
}
public String getSchema() throws SQLException {
return super.getCurrentSchemaName();
}
public void abort(Executor executor) throws SQLException {
// TODO Auto-generated method stub
}
public void setNetworkTimeout(Executor executor, int milliseconds)
throws SQLException {
// TODO Auto-generated method stub
}
public int getNetworkTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
}