Java Code Examples for java.sql.Connection#getClass()

The following examples show how to use java.sql.Connection#getClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ErrorPositionExtractor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Extract location information for Informix DB.
 *
 * <p>
 * For Informix the location information is not present in the exception
 * itself. It has to be extracted from the connection.</p>
 *
 * <p>
 * In this case the connection is accessed via reflection to call the
 * corresponding method.</p>
 *
 * <p>
 * In case connection gets wrapped later it needs to be unwrapped here.</p>
 */
private static int extractErrorPositionForInformix(Connection con, Statement stmt, Throwable ex, String sql) {
    // try to get exact position from exception message
    if (ex == null) {
        return -1;
    }

    Class connectionClass = con.getClass();
    if (connectionClass.getName().startsWith("com.informix.jdbc")) {
        try {
            Method getSQLStatementOffset = connectionClass.getMethod("getSQLStatementOffset");
            int column = (Integer) getSQLStatementOffset.invoke(con);
            if (column <= 0) {
                return -1;
            } else {
                return column - 1;
            }
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException innerEx) {
            LOG.log(Level.FINE, "Failed to extract informix error location", innerEx);
            return -1;
        }
    } else {
        return -1;
    }
}
 
Example 2
Source File: JdbcIntermediateFacade.java    From dekaf with Apache License 2.0 6 votes vote down vote up
/**
 * Try to call the "Connection#getSchema()" function
 * that appears in JRE 1.7.
 *
 * Some JDBC vendor can also ignore this method or throw exceptions.
 */
@Nullable
private static String getSchema(final Connection connection) {
  String schemaName = null;
  try {
    final Class<? extends Connection> connectionClass = connection.getClass();
    final Method getSchemaMethod = connectionClass.getMethod("getSchema");
    if (getSchemaMethod != null) {
      schemaName = (String) getSchemaMethod.invoke(connection);
    }
  }
  catch (NoSuchMethodException nsm) {
    // no such method. sad
  }
  catch (Exception e) {
    // TODO log this somehow?
  }
  return schemaName;
}
 
Example 3
Source File: JdbcHelperImpl.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Nullable
private ConnectionMetaData getConnectionMetaData(Connection connection) {
    ConnectionMetaData connectionMetaData = metaDataMap.get(connection);
    if (connectionMetaData != null) {
        return connectionMetaData;
    }

    Class<?> type = connection.getClass();
    Boolean supported = isSupported(metadataSupported, type);
    if (supported == Boolean.FALSE) {
        return null;
    }

    try {
        DatabaseMetaData metaData = connection.getMetaData();
        connectionMetaData = ConnectionMetaData.create(metaData.getURL(), metaData.getUserName());
        if (supported == null) {
            markSupported(metadataSupported, type);
        }
    } catch (SQLException e) {
        markNotSupported(metadataSupported, type, e);
    }

    if (connectionMetaData != null) {
        metaDataMap.put(connection, connectionMetaData);
    }
    return connectionMetaData;
}
 
Example 4
Source File: DefaultSchemaNameResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private SchemaNameResolver determineAppropriateResolverDelegate(Connection connection) {
	// unfortunately Connection#getSchema is only available in Java 1.7 and above
	// and Hibernate still baselines on 1.6.  So for now, use reflection and
	// leverage the Connection#getSchema method if it is available.
	try {
		final Class<? extends Connection> jdbcConnectionClass = connection.getClass();
		final Method getSchemaMethod = jdbcConnectionClass.getMethod( "getSchema" );
		if ( getSchemaMethod != null && getSchemaMethod.getReturnType().equals( String.class ) ) {
			try {
				// If the JDBC driver does not implement the Java 7 spec, but the JRE is Java 7
				// then the getSchemaMethod is not null but the call to getSchema() throws an java.lang.AbstractMethodError
				connection.getSchema();
				return new SchemaNameResolverJava17Delegate();
			}
			catch (java.lang.AbstractMethodError e) {
				log.debugf( "Unable to use Java 1.7 Connection#getSchema" );
				return SchemaNameResolverFallbackDelegate.INSTANCE;
			}
		}
		else {
			log.debugf( "Unable to use Java 1.7 Connection#getSchema" );
			return SchemaNameResolverFallbackDelegate.INSTANCE;
		}
	}
	catch (Exception ignore) {
		log.debugf(
				"Unable to use Java 1.7 Connection#getSchema : An error occurred trying to resolve the connection default schema resolver: "
						+ ignore.getMessage() );
		return SchemaNameResolverFallbackDelegate.INSTANCE;
	}
}
 
Example 5
Source File: TransactionRomanticSnapshotBuilder.java    From lastaflute with Apache License 2.0 5 votes vote down vote up
protected Object digUpDbmsNativeProcessIdOf(Connection physicalConn) {
    final Class<?> connType = physicalConn.getClass();
    if (getMySQLConnectionClassFQCN().equals(connType.getName())) {
        final Method method = DfReflectionUtil.getPublicMethod(connType, "getId", (Class<?>[]) null);
        return DfReflectionUtil.invoke(method, physicalConn, (Object[]) null);
    }
    return null;
}
 
Example 6
Source File: My_Transaction.java    From tddl with Apache License 2.0 5 votes vote down vote up
private static TGroupConnection getTGroupConnection(Connection con) {
    if (con instanceof TGroupConnection) {
        return (TGroupConnection) con;
    }

    throw new RuntimeException("impossible,connection is not TGroupConnection:" + con.getClass());
}
 
Example 7
Source File: EncodingUtils.java    From tddl5 with Apache License 2.0 4 votes vote down vote up
public static String getEncoding(Connection conn) {
    if (conn instanceof IConnection) {
        return ((IConnection) conn).getEncoding();
    }

    if (conn instanceof TConnectionWrapper) {
        conn = ((TConnectionWrapper) conn).getTargetConnection();
        return getEncoding(conn);
    }

    if (conn instanceof DruidPooledConnection) {
        conn = ((DruidPooledConnection) conn).getConnection();
        return getEncoding(conn);
    }

    try {
        final Class<?> clazz = conn.getClass();
        Method getMethod = getMethodCaches.get(clazz, new Callable<Method>() {

            @Override
            public Method call() throws Exception {

                Class c = clazz;
                while (true) {
                    try {
                        Method setMethod = c.getDeclaredMethod("getEncoding", new Class[] {});
                        if (setMethod != null) {
                            setMethod.setAccessible(true);
                        }
                        return setMethod;
                    } catch (Exception ex) {
                    }

                    c = c.getSuperclass();

                    if (c == null) {
                        throw new TddlRuntimeException(ErrorCode.ERR_EXECUTOR, "get getEncoding error, clazz:"
                                                                               + clazz);
                    }
                }
            }
        });

        return (String) getMethod.invoke(conn, new Object[] {});
    } catch (Throwable e) {
        throw new TddlNestableRuntimeException(e);
    }
}