下面列出了java.sql.Connection#getClass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
}
/**
* 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;
}
@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;
}
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;
}
}
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;
}
private static TGroupConnection getTGroupConnection(Connection con) {
if (con instanceof TGroupConnection) {
return (TGroupConnection) con;
}
throw new RuntimeException("impossible,connection is not TGroupConnection:" + con.getClass());
}
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);
}
}