вторник, 8 февраля 2011 г.

C3P0ConnectionExtractor

Полезный класс для тех, кому нужен OracleConnection в чистом виде, а доступен только C3P0ProxyConnection.
C3P0ConnectionExtractor - сделан на базе Spring. Тестировался с с3з0-0.9.1 (почему-то тот extractor, что в Spring-е, не заработал).

import com.mchange.v2.c3p0.C3P0ProxyConnection;
import org.hibernate.jdbc.BorrowedConnectionProxy;
import org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractorAdapter;

import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;

public class C3P0ConnectionExtractor extends NativeJdbcExtractorAdapter {

/*===========================================[ INSTANCE VARIABLES ]=========*/

private final Method getRawConnectionMethod;

/*===========================================[ CONSTRUCTORS ]===============*/


public C3P0ConnectionExtractor() {
try {
getRawConnectionMethod = getClass().getMethod("getRawConnection", new Class[]{Connection.class});
}
catch (NoSuchMethodException ex) {
throw new IllegalStateException("Internal error in C3P0ConnectionExtractor: " + ex.getMessage());
}
}

/*===========================================[ CLASS METHODS ]==============*/

protected Connection doGetNativeConnection(Connection con) throws SQLException {
try {
Connection connection = BorrowedConnectionProxy.getWrappedConnection(con);
if (connection instanceof C3P0ProxyConnection) {
C3P0ProxyConnection cpCon = (C3P0ProxyConnection) connection;
return (Connection) cpCon.rawConnectionOperation(
getRawConnectionMethod, null, new Object[]{C3P0ProxyConnection.RAW_CONNECTION});
}
return con;
} catch (Exception ignored) {
return con;
}
}

public static Connection getRawConnection(Connection con) {
return con;
}
}

Комментариев нет:

Отправить комментарий