开发者

GlassFish 3.1.1 - getting jdbc connection

开发者 https://www.devze.com 2023-04-07 09:15 出处:网络
I\'m trying to get开发者_如何学C OracleConnection from glassfish by this lines: EntityManager em = getEntityManager();

I'm trying to get开发者_如何学C OracleConnection from glassfish by this lines:

EntityManager em = getEntityManager();
Connection c = em.unwrap(Connection.class);

But, in debugger I see that the actual class for c is ConnectionHolder40 class. Where I can find a jar with ConnectionHolder40 definition?


You can get only interfaces. If you use the Connection interface, you will get a first wrapper in hierarchy that implements Connection, so ConnectionHolder40 is that case. If you want to get OracleConnection - and I see here that it is an interface, you should ask for it.

But you need a JDBC4 driver. With JDBC3 drivers you can have problems (as I have with Informix 3.70) because they do not implement unwrap and isWrapperFor methods and also ConnectionHolder40, *StatementWrapper40 and ResultSetWrapper40 are not implemented correctly. If I want to unwrap IfmxConnection from holder, I can. But I cannot ask holder if he is a wrapper for IfmxConnection - it causes exception because it tries to ask the driver implementation: ConnectionHolder40 . StatementWrapper and ResultSetWrapper throw exception from both methods if they don't directly implement interface (exactly, if you don't ask for java.sql.*Statement resp. java.sql.ResultSet).


Try

public OracleConnection getOracleConnection(Connection connection) throws SQLException {
    OracleConnection oconn = null;
    try {
        if (connection.isWrapperFor(oracle.jdbc.OracleConnection.class)) {
            oconn = (OracleConnection) connection.unwrap(oracle.jdbc.OracleConnection.class)._getPC();
        }
    } catch (SQLException e) {
        throw e;
    }
    return oconn;
}


java.sql.Connection is an interface, ConnectionHolder40 is just the connection wrapper that is used by Glassfish, it is probably a generated class, so will not be in any jar file.

You should only use the Connection API, so should not need the class.

JDBC also supports an unwrap API if you want to get the real JDBC driver connection.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号