List of usage examples for java.sql Connection getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:org.jboss.dashboard.database.hibernate.LOBHelper.java
public void oracleNullSafeSet(PreparedStatement statement, Object value, int index, ValueWriter vw) throws HibernateException, SQLException { try {//from w w w .j a va2 s . co m // Invoke by reflection the Oracle classes Class oracleBlobClass = Class.forName(ORACLE_CLASS); Method createTempMethod = getMethod(oracleBlobClass, ORACLE_TEMP_METHOD, Connection.class, Boolean.TYPE, Integer.TYPE); Field durationSession = oracleBlobClass.getField(ORACLE_DURATION__SESSION); Object arglist[] = new Object[3]; Connection conn = statement.getConnection(); arglist[0] = conn; arglist[1] = Boolean.TRUE; arglist[2] = durationSession.get(null); Object tempBlob = null; // Needed to avoid JBoss AS class loading issues... Class connClassInCurrentClassLoader = Class.forName(conn.getClass().getName()); // Direct Oracle connection if (Class.forName(ORACLE_JDBC_ORACLE_CONNECTION).isAssignableFrom(connClassInCurrentClassLoader)) { tempBlob = createTempMethod.invoke(null, arglist); // null is valid because of static method } // JBoss AS data source wrapper connection. else if (WrappedConnection.class.isAssignableFrom(connClassInCurrentClassLoader)) { arglist[0] = ReflectionUtils.invokeMethod(conn, "getUnderlyingConnection", null); tempBlob = createTempMethod.invoke(null, arglist); // null is valid because of static method } // C3P0 pool managed connection. else if (NewProxyConnection.class.isAssignableFrom(connClassInCurrentClassLoader)) { NewProxyConnection castCon = (NewProxyConnection) conn; arglist[0] = C3P0ProxyConnection.RAW_CONNECTION; tempBlob = castCon.rawConnectionOperation(createTempMethod, C3P0ProxyConnection.RAW_CONNECTION, arglist); } // Apache's DBCP pool managed connection. else if (PoolableConnection.class.isAssignableFrom(connClassInCurrentClassLoader)) { arglist[0] = ((PoolableConnection) statement.getConnection()).getDelegate(); tempBlob = createTempMethod.invoke(null, arglist); // null is valid because of static method } else { boolean throwException = true; //check if we are running in websphere try { String wasConnectionWrapper = "com.ibm.ws.rsadapter.jdbc.WSJdbcConnection"; Class wasConnectionWrapperClass = Class.forName(wasConnectionWrapper); if (wasConnectionWrapperClass.isAssignableFrom(connClassInCurrentClassLoader)) { String helperClass = "com.ibm.websphere.rsadapter.WSCallHelper"; Class helper = Class.forName(helperClass); Method nativeConnMethod = helper.getMethod("getNativeConnection", new Class[] { Object.class }); Connection nativeConn = (Connection) nativeConnMethod.invoke(null, conn); if (Class.forName(ORACLE_JDBC_ORACLE_CONNECTION).isAssignableFrom(nativeConn.getClass())) { arglist[0] = nativeConn; tempBlob = createTempMethod.invoke(null, arglist); throwException = false; } } } catch (Throwable e) { e.printStackTrace(); // do nothing } if (throwException) { throw new HibernateException("JDBC connection object must be a oracle.jdbc.OracleConnection " + "a " + WrappedConnection.class.getName() + "a " + PoolableConnection.class.getName() + "or a " + NewProxyConnection.class.getName() + ". Connection class is " + connClassInCurrentClassLoader.getName()); } } Method openMethod = getMethod(oracleBlobClass, ORACLE_OPEN_METHOD, Integer.TYPE, null, null); Field fieldReadWrite = oracleBlobClass.getField(ORACLE_MODE__READWRITE); arglist = new Object[1]; arglist[0] = fieldReadWrite.get(null); //null is valid because of static field openMethod.invoke(tempBlob, arglist); Method getOutputStreamMethod = oracleBlobClass.getDeclaredMethod(ORACLE_GET_BINARY_OUTPUT_STREAM, null); OutputStream os = (OutputStream) getOutputStreamMethod.invoke(tempBlob, null); try { vw.writeValue(os, value); os.flush(); } finally { os.close(); } Method closeMethod = oracleBlobClass.getDeclaredMethod(ORACLE_CLOSE, null); closeMethod.invoke(tempBlob, null); statement.setBlob(index, (Blob) tempBlob); } catch (Exception e) { throw new HibernateException("Error in oracleNullSafeSet", e); } }
From source file:org.jboss.dashboard.database.NonPooledDataSource.java
public Connection createConnectionProxy(Connection conn) throws SQLException { return (Connection) Proxy.newProxyInstance(conn.getClass().getClassLoader(), getClassInterfaces(conn.getClass()), new ConnectionInvocationHandler(conn)); }
From source file:org.nuxeo.runtime.datasource.ConnectionHelper.java
/** * Tries to unwrap the connection to get the real physical one (returned by the original datasource). * <p>//from w ww. j a v a 2 s .co m * This should only be used by code that needs to cast the connection to a driver-specific class to use * driver-specific features. * * @throws SQLException if no actual physical connection was allocated yet */ public static Connection unwrap(Connection connection) throws SQLException { if (connection instanceof org.tranql.connector.jdbc.ConnectionHandle) { return ((org.tranql.connector.jdbc.ConnectionHandle) connection).getAssociation() .getPhysicalConnection(); } // now try Apache DBCP unwrap (standard or Tomcat), to skip datasource wrapping layers // this needs accessToUnderlyingConnectionAllowed=true in the pool config try { Method m = connection.getClass().getMethod("getInnermostDelegate"); m.setAccessible(true); // needed, method of inner private class Connection delegate = (Connection) m.invoke(connection); if (delegate == null) { log.error("Cannot access underlying connection, you must use " + "accessToUnderlyingConnectionAllowed=true in the pool configuration"); } else { connection = delegate; } } catch (NoSuchMethodException | SecurityException | IllegalAccessException | InvocationTargetException e) { // ignore missing method, connection not coming from Apache pool } return connection; }
From source file:org.ofbiz.tenant.util.TenantUtil.java
/** * is connection available// www .j a v a2 s. co m * @param tenantId * @param delegator * @return */ public static boolean isConnectionAvailable(String tenantId, Delegator delegator) { try { List<GenericValue> tenantDataSources = delegator.findByAnd("TenantDataSource", UtilMisc.toMap("tenantId", tenantId), null, false); for (GenericValue tenantDataSource : tenantDataSources) { String entityGroupName = tenantDataSource.getString("entityGroupName"); String jdbcUri = tenantDataSource.getString("jdbcUri"); String jdbcUsername = tenantDataSource.getString("jdbcUsername"); String jdbcPassword = tenantDataSource.getString("jdbcPassword"); GenericHelperInfo helperInfo = delegator.getGroupHelperInfo(entityGroupName); Connection connection = ConnectionFactory.getConnection(jdbcUri, jdbcUsername, jdbcPassword); ManagedConnection managedConn = (ManagedConnection) ConnectionFactory.getConnection(helperInfo); PoolableConnection poolConn = (PoolableConnection) managedConn.getDelegate(); Connection innermostDelegate = poolConn.getInnermostDelegate(); if (UtilValidate.isNotEmpty(connection)) { if (!innermostDelegate.getClass().getName().equals(connection.getClass().getName())) { return false; } } else { return false; } } } catch (Exception e) { Debug.logWarning(e, module); return false; } return true; }
From source file:org.openkoala.koala.monitor.support.JdbcPoolStatusCollector.java
/** * ?// w w w. jav a 2 s. c o m * @param conn * @return */ private String getConnectionId(Connection conn) { if (conn.getClass().getName().contains("Proxool")) { return conn.toString().split("\\(")[1].replace(")", ""); } return null; }
From source file:org.springframework.jdbc.support.lob.OracleLobHandler.java
/** * Retrieve the {@code oracle.sql.BLOB} and {@code oracle.sql.CLOB} * classes via reflection, and initialize the values for the * DURATION_SESSION, MODE_READWRITE and MODE_READONLY constants defined there. * <p><strong>See Also:</strong> * <ul>/*ww w . j a va 2s . co m*/ * <li><a href="http://download.oracle.com/otn_hosted_doc/jdeveloper/905/jdbc-javadoc/oracle/sql/BLOB.html#DURATION_SESSION">oracle.sql.BLOB.DURATION_SESSION</a></li> * <li><a href="http://download.oracle.com/otn_hosted_doc/jdeveloper/905/jdbc-javadoc/oracle/sql/BLOB.html#MODE_READWRITE">oracle.sql.BLOB.MODE_READWRITE</a></li> * <li><a href="http://download.oracle.com/otn_hosted_doc/jdeveloper/905/jdbc-javadoc/oracle/sql/BLOB.html#MODE_READONLY">oracle.sql.BLOB.MODE_READONLY</a></li> * <li><a href="http://download.oracle.com/otn_hosted_doc/jdeveloper/905/jdbc-javadoc/oracle/sql/CLOB.html#DURATION_SESSION">oracle.sql.CLOB.DURATION_SESSION</a></li> * <li><a href="http://download.oracle.com/otn_hosted_doc/jdeveloper/905/jdbc-javadoc/oracle/sql/CLOB.html#MODE_READWRITE">oracle.sql.CLOB.MODE_READWRITE</a></li> * <li><a href="http://download.oracle.com/otn_hosted_doc/jdeveloper/905/jdbc-javadoc/oracle/sql/CLOB.html#MODE_READONLY">oracle.sql.CLOB.MODE_READONLY</a></li> * </ul> * @param con the Oracle Connection, for using the exact same class loader * that the Oracle driver was loaded with */ protected synchronized void initOracleDriverClasses(Connection con) { if (this.blobClass == null) { try { // Initialize oracle.sql.BLOB class this.blobClass = con.getClass().getClassLoader().loadClass(BLOB_CLASS_NAME); this.durationSessionConstants.put(this.blobClass, this.blobClass.getField(DURATION_SESSION_FIELD_NAME).getInt(null)); this.modeReadWriteConstants.put(this.blobClass, this.blobClass.getField(MODE_READWRITE_FIELD_NAME).getInt(null)); this.modeReadOnlyConstants.put(this.blobClass, this.blobClass.getField(MODE_READONLY_FIELD_NAME).getInt(null)); // Initialize oracle.sql.CLOB class this.clobClass = con.getClass().getClassLoader().loadClass(CLOB_CLASS_NAME); this.durationSessionConstants.put(this.clobClass, this.clobClass.getField(DURATION_SESSION_FIELD_NAME).getInt(null)); this.modeReadWriteConstants.put(this.clobClass, this.clobClass.getField(MODE_READWRITE_FIELD_NAME).getInt(null)); this.modeReadOnlyConstants.put(this.clobClass, this.clobClass.getField(MODE_READONLY_FIELD_NAME).getInt(null)); } catch (Exception ex) { throw new InvalidDataAccessApiUsageException( "Couldn't initialize OracleLobHandler because Oracle driver classes are not available. " + "Note that OracleLobHandler requires Oracle JDBC driver 9i or higher!", ex); } } }
From source file:org.springframework.jdbc.support.nativejdbc.WebSphereNativeJdbcExtractor.java
/** * Retrieve the Connection via WebSphere's <code>getNativeConnection</code> method. */// w w w .ja v a 2 s. com protected Connection doGetNativeConnection(Connection con) throws SQLException { // WebSphere 5 connection? if (this.webSphere5ConnectionClass != null && this.webSphere5ConnectionClass.isAssignableFrom(con.getClass())) { try { // WebSphere 5's WSJdbcUtil.getNativeConnection(wsJdbcConnection) return (Connection) this.webSphere5NativeConnectionMethod.invoke(null, new Object[] { con }); } catch (InvocationTargetException ex) { throw new DataAccessResourceFailureException("WebSphere5's getNativeConnection method failed", ex.getTargetException()); } catch (Exception ex) { throw new DataAccessResourceFailureException( "Could not access WebSphere5's getNativeConnection method", ex); } } // WebSphere 4 connection (or version 4 connection on WebSphere 5)? else if (this.webSphere4ConnectionClass != null && this.webSphere4ConnectionClass.isAssignableFrom(con.getClass())) { try { // WebSphere 4's connectionProxy.getPhysicalConnection() return (Connection) this.webSphere4PhysicalConnectionMethod.invoke(con, (Object[]) null); } catch (InvocationTargetException ex) { throw new DataAccessResourceFailureException("WebSphere4's getPhysicalConnection method failed", ex.getTargetException()); } catch (Exception ex) { throw new DataAccessResourceFailureException( "Could not access WebSphere4's getPhysicalConnection method", ex); } } // No known WebSphere connection -> return as-is. else { if (logger.isDebugEnabled()) { logger.debug("Connection [" + con + "] is not a WebSphere 5/4 connection, returning as-is"); } return con; } }
From source file:org.talend.core.model.metadata.builder.database.ExtractMetaDataUtils.java
/** * DOC cantoine. Method to return DatabaseMetaData of a DB connection. * /*w w w. ja v a 2s . c o m*/ * @param Connection conn * @param isSqlMode whether is sqlMode * @param String dbType * @return DatabaseMetaData * * */ public DatabaseMetaData getDatabaseMetaData(Connection conn, String dbType, boolean isSqlMode, String database) { DatabaseMetaData dbMetaData = null; try { if (conn != null && !conn.isClosed()) { // MOD sizhaoliu 2012-5-21 TDQ-4884 if (MSSQL_CONN_CLASS.equals(conn.getClass().getName())) { dbMetaData = createJtdsDatabaseMetaData(conn); } else if (EDatabaseTypeName.IBMDB2ZOS.getXmlName().equals(dbType)) { dbMetaData = createDB2ForZosFakeDatabaseMetaData(conn); } else if (EDatabaseTypeName.TERADATA.getXmlName().equals(dbType) && isSqlMode) { dbMetaData = createTeradataFakeDatabaseMetaData(conn); // add by wzhang for bug 8106. set database name for teradata. TeradataDataBaseMetadata teraDbmeta = (TeradataDataBaseMetadata) dbMetaData; teraDbmeta.setDatabaseName(database); } else if (EDatabaseTypeName.SAS.getXmlName().equals(dbType)) { dbMetaData = createSASFakeDatabaseMetaData(conn); } else if (EDatabaseTypeName.SYBASEASE.getDisplayName().equals(dbType) || SYBASE_DATABASE_PRODUCT_NAME.equals(dbType)) { // TDQ-8300 make sybaseIQ work well,currently,the dbType of sybaseIQ is still // "Sybase (ASE and IQ)",so use dbMetaData.getDatabaseProductName() to judge if it is sybaseIQ . dbMetaData = conn.getMetaData(); if (dbMetaData != null && EDatabaseTypeName.SYBASEIQ.getDisplayName() .equals(dbMetaData.getDatabaseProductName())) { dbMetaData = createSybaseIQFakeDatabaseMetaData(conn); } else { dbMetaData = createSybaseFakeDatabaseMetaData(conn); } } else if (EDatabaseTypeName.HIVE.getDisplayName().equals(dbType) && isHiveEmbeddedConn(conn)) { // dbMetaData = new EmbeddedHiveDataBaseMetadata(conn); } else if (EDatabaseTypeName.AS400.getXmlName().equals(dbType)) { dbMetaData = createAS400FakeDatabaseMetaData(conn); } else { dbMetaData = conn.getMetaData(); } } } catch (SQLException e) { log.error(e.toString()); throw new RuntimeException(e); } catch (Exception e) { log.error(e.toString()); throw new RuntimeException(e); } return dbMetaData; }
From source file:org.talend.core.model.metadata.builder.database.ExtractMetaDataUtils.java
public boolean isHiveConnection(Connection hiveConn) { if (hiveConn != null) { Class<?> clazz = hiveConn.getClass(); if ("HiveConnection".equals(clazz.getSimpleName())) { //$NON-NLS-1$ return true; }//from w w w .ja v a 2 s . c o m } return false; }
From source file:org.talend.core.model.metadata.builder.database.ExtractMetaDataUtils.java
public boolean isHiveEmbeddedConn(Connection hiveConn) { if (hiveConn != null) { Class<?> clazz = hiveConn.getClass(); if ("HiveConnection".equals(clazz.getSimpleName())) { //$NON-NLS-1$ try { Field clientField = clazz.getDeclaredField("client"); //$NON-NLS-1$ clientField.setAccessible(true); Object clientObj = clientField.get(hiveConn); if (clientObj != null) { Class<?> clientClass = clientObj.getClass(); if ("HiveServerHandler".equals(clientClass.getSimpleName())) { //$NON-NLS-1$ return true; }//from w ww . j a v a2 s .co m } } catch (Exception e) { return false; } } } return false; }