1 21 22 package org.apache.derby.client.net; 23 24 import java.sql.Array ; 25 import org.apache.derby.client.am.SQLExceptionFactory; 26 import org.apache.derby.client.am.SqlException; 27 import java.sql.Blob ; 28 import java.sql.Clob ; 29 import java.sql.Connection ; 30 import java.sql.NClob ; 31 import java.sql.PreparedStatement ; 32 import java.sql.ResultSet ; 33 import java.sql.SQLClientInfoException ; 34 import java.sql.SQLException ; 35 import java.sql.SQLXML ; 36 import java.sql.Struct ; 37 import java.util.HashMap ; 38 import java.util.Map ; 39 import java.util.Properties ; 40 import java.util.Enumeration ; 41 import org.apache.derby.client.ClientPooledConnection; 42 import org.apache.derby.client.am.ClientMessageId; 43 import org.apache.derby.client.am.FailedProperties40; 44 import org.apache.derby.shared.common.reference.SQLState; 45 46 public class NetConnection40 extends org.apache.derby.client.net.NetConnection { 47 52 private PreparedStatement isValidStmt = null; 53 54 59 60 public NetConnection40(NetLogWriter netLogWriter, 61 String databaseName, 62 java.util.Properties properties) throws SqlException { 63 super(netLogWriter,databaseName,properties); 64 } 65 public NetConnection40(NetLogWriter netLogWriter, 66 org.apache.derby.jdbc.ClientBaseDataSource dataSource, 67 String user, 68 String password) throws SqlException { 69 super(netLogWriter,dataSource,user,password); 70 } 71 public NetConnection40(NetLogWriter netLogWriter, 72 int driverManagerLoginTimeout, 73 String serverName, 74 int portNumber, 75 String databaseName, 76 java.util.Properties properties) throws SqlException{ 77 super(netLogWriter,driverManagerLoginTimeout,serverName,portNumber,databaseName,properties); 78 } 79 public NetConnection40(NetLogWriter netLogWriter, 80 String user, 81 String password, 82 org.apache.derby.jdbc.ClientBaseDataSource dataSource, 83 int rmId, 84 boolean isXAConn) throws SqlException{ 85 super(netLogWriter,user,password,dataSource,rmId,isXAConn); 86 } 87 public NetConnection40(NetLogWriter netLogWriter, 88 String ipaddr, 89 int portNumber, 90 org.apache.derby.jdbc.ClientBaseDataSource dataSource, 91 boolean isXAConn) throws SqlException{ 92 super(netLogWriter,ipaddr,portNumber,dataSource,isXAConn); 93 } 94 95 96 117 118 public NetConnection40(NetLogWriter netLogWriter, 119 String user, 120 String password, 121 org.apache.derby.jdbc.ClientBaseDataSource dataSource, 122 int rmId, 123 boolean isXAConn, 124 ClientPooledConnection cpc) throws SqlException{ 125 super(netLogWriter,user,password,dataSource,rmId,isXAConn,cpc); 126 } 127 128 129 130 public Array createArrayOf(String typeName, Object [] elements) 131 throws SQLException { 132 throw SQLExceptionFactory.notImplemented ("createArrayOf(String,Object[])"); 133 } 134 135 143 144 public Clob createClob() throws SQLException { 145 try { 146 checkForClosedConnection(); 147 } catch (SqlException se) { 148 throw se.getSQLException(); 149 } 150 org.apache.derby.client.am.Clob clob = new org.apache.derby.client.am.Clob(this.agent_,""); 151 return clob; 152 } 153 154 162 163 public Blob createBlob() throws SQLException { 164 try { 165 checkForClosedConnection(); 166 } catch (SqlException se) { 167 throw se.getSQLException(); 168 } 169 org.apache.derby.client.am.Blob blob = new org.apache.derby.client.am.Blob(new byte[0],this.agent_, 0); 170 return blob; 171 } 172 173 public NClob createNClob() throws SQLException { 174 throw SQLExceptionFactory.notImplemented ("createNClob ()"); 175 } 176 177 public SQLXML createSQLXML() throws SQLException { 178 throw SQLExceptionFactory.notImplemented ("createSQLXML ()"); 179 } 180 181 public Struct createStruct(String typeName, Object [] attributes) 182 throws SQLException { 183 throw SQLExceptionFactory.notImplemented ("createStruct(String,Object[])"); 184 } 185 186 208 public boolean isValid(int timeout) throws SQLException { 209 if (timeout < 0) { 211 throw new SqlException(agent_.logWriter_, 212 new ClientMessageId(SQLState.INVALID_API_PARAMETER), 213 new Integer (timeout), "timeout", 214 "java.sql.Connection.isValid" ).getSQLException(); 215 } 216 217 if (isClosed()) { 219 return false; 220 } 221 222 synchronized(this) { 224 try { 225 int oldTimeout = netAgent_.getTimeout(); 227 228 netAgent_.setTimeout(timeout); 230 231 if (isValidStmt == null) { 234 isValidStmt = prepareStatement("VALUES (1)"); 235 } 236 237 isValidStmt.setQueryTimeout(timeout); 239 240 ResultSet rs = isValidStmt.executeQuery(); 242 rs.close(); 243 244 netAgent_.setTimeout(oldTimeout); 246 } catch(SQLException e) { 247 return false; 250 } 251 } 252 253 return true; } 255 256 260 synchronized public void close() throws SQLException { 261 if (isValidStmt != null) { 263 isValidStmt.close(); 264 isValidStmt = null; 265 } 266 super.close(); 267 } 268 269 278 public void setClientInfo(String name, String value) 279 throws SQLClientInfoException { 280 Properties p = FailedProperties40.makeProperties(name,value); 281 try { checkForClosedConnection(); } 282 catch (SqlException se) { 283 throw new SQLClientInfoException 284 (se.getMessage(), se.getSQLState(), 285 new FailedProperties40(p).getProperties()); 286 } 287 288 if (name == null && value == null) { 289 return; 290 } 291 setClientInfo(p); 292 } 293 294 308 public void setClientInfo(Properties properties) 309 throws SQLClientInfoException { 310 FailedProperties40 fp = new FailedProperties40(properties); 311 try { checkForClosedConnection(); } 312 catch (SqlException se) { 313 throw new SQLClientInfoException (se.getMessage(), se.getSQLState(), 314 fp.getProperties()); 315 } 316 317 if (properties == null || properties.isEmpty()) { 318 return; 319 } 320 321 SqlException se = 322 new SqlException(agent_.logWriter_, 323 new ClientMessageId 324 (SQLState.PROPERTY_UNSUPPORTED_CHANGE), 325 fp.getFirstKey(), fp.getFirstValue()); 326 throw new SQLClientInfoException (se.getMessage(), 327 se.getSQLState(), fp.getProperties()); 328 } 329 330 339 public String getClientInfo(String name) 340 throws SQLException { 341 try { 342 checkForClosedConnection(); 343 return null; 344 } 345 catch (SqlException se) { throw se.getSQLException(); } 346 } 347 348 356 public Properties getClientInfo() 357 throws SQLException { 358 try { 359 checkForClosedConnection(); 360 return new Properties (); 361 } 362 catch (SqlException se) { throw se.getSQLException(); } 363 } 364 365 366 372 public final Map <String , Class <?>> getTypeMap() throws SQLException { 373 Map typeMap = super.getTypeMap(); 379 if (typeMap == null) return null; 380 Map <String , Class <?>> genericTypeMap = new HashMap <String , Class <?>>(); 381 for (Object key : typeMap.keySet()) { 382 genericTypeMap.put((String ) key, (Class ) typeMap.get(key)); 383 } 384 return genericTypeMap; 385 } 386 387 398 public boolean isWrapperFor(Class <?> interfaces) throws SQLException { 399 try { 400 checkForClosedConnection(); 401 } catch (SqlException se) { 402 throw se.getSQLException(); 403 } 404 return interfaces.isInstance(this); 405 } 406 407 415 public <T> T unwrap(java.lang.Class <T> interfaces) 416 throws SQLException { 417 try { 418 checkForClosedConnection(); 419 return interfaces.cast(this); 420 } catch (ClassCastException cce) { 421 throw new SqlException(null, 422 new ClientMessageId(SQLState.UNABLE_TO_UNWRAP), 423 interfaces).getSQLException(); 424 } catch (SqlException se) { 425 throw se.getSQLException(); 426 } 427 } 428 429 } 430 | Popular Tags |