从tomcat连接池和JPA中提取postgres连接对象

时间:2013-06-30 19:56:03

标签: postgresql jdbc tomcat7

我有一个在Tomcat中运行的Java EE应用程序,使用Tomcat连接池,在postgres数据库上运行,并且该应用程序使用OpenJPA进行ORM。

对于某些操作,我需要从postgres访问LargeObjectManager API。这需要我引用实际的postgres连接对象。

所以,我正在尝试深入研究我从OpenJPA获得的连接对象,以获得实际的Postgres。但我被一个'sun.misc.proxy。$ ProxyNN'对象拦住了,我不知道怎么能进一步。

以下是我现在正在做的事情:

OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
// get open JPA connection object
Connection c = oem.getConnection();
// all the way down to what's no longer OpenJPA
c = ((DelegatingConnection)c).getInnermostDelegate()

这就是我停下来的地方。返回的对象如下。我无法在其上调用ProxyConnection方法,我甚至不知道是谁创建了这些代理(但我敢打赌它是Tomcat DBCP)。

c = {com.sun.proxy.$Proxy5@1793}"ProxyConnection[PooledConnection[Pooled connection wrapping physical connection org.postgresql.jdbc4.Jdbc4Connection@a7a434]]"
   h = {org.apache.tomcat.jdbc.pool.ProxyConnection@5671}"ProxyConnection[PooledConnection[Pooled connection wrapping physical connection org.postgresql.jdbc4.Jdbc4Connection@a7a434]]"
    connection = {org.apache.tomcat.jdbc.pool.PooledConnection@5673}"PooledConnection[Pooled connection wrapping physical connection org.postgresql.jdbc4.Jdbc4Connection@a7a434]"
    poolProperties = {org.apache.tomcat.jdbc.pool.PoolProperties@5676}"ConnectionPool[defaultAutoCommit=false; defaultReadOnly=null; defaultTransactionIsolation=-1; defaultCatalog=null; driverClassName=null; maxActive=100; maxIdle=100; minIdle=10; initialSize=10; maxWait=30000; testOnBorrow=false; testOnReturn=false; timeBe...
      connection = {com.sun.proxy.$Proxy0@5677}"Pooled connection wrapping physical connection org.postgresql.jdbc4.Jdbc4Connection@a7a434"
        h = {org.postgresql.ds.jdbc23.AbstractJdbc23PooledConnection$ConnectionHandler@5686}
          xaConnection = null
          abandonTrace = null
          timestamp = 1372621172141
          lock = {java.util.concurrent.locks.ReentrantReadWriteLock@5678}"java.util.concurrent.locks.ReentrantReadWriteLock@9267fe[Write locks = 0, Read locks = 0]"
          discarded = false
          lastConnected = 1372620679402
          lastValidated = 1372620679398
          parent = {org.apache.tomcat.jdbc.pool.ConnectionPool@5674}
          attributes = {java.util.HashMap@5679} size = 2
          handler = {org.apache.tomcat.jdbc.pool.ProxyConnection@5671}"ProxyConnection[PooledConnection[Pooled connection wrapping physical connection org.postgresql.jdbc4.Jdbc4Connection@a7a434]]"
          released = {java.util.concurrent.atomic.AtomicBoolean@5680}"false"
          suspect = false
          driver = null
          pool = {org.apache.tomcat.jdbc.pool.ConnectionPool@5674}
          properties = null
          next = null
          useEquals = true

tomcat:7.0.22 postgres jdbc:9.2-1002 openJPA:2.1.1

1 个答案:

答案 0 :(得分:2)

尝试一下,可能需要一些调整,但我相信它应该按原样运行,或者至少给你一些有用的指针,指出需要解开的代理在哪里


//unwrap proxy invocation handler
org.apache.tomcat.jdbc.pool.ProxyConnection tomcatProxy = (ProxyConnection) Proxy.getInvocationHandler(c);

org.apache.tomcat.jdbc.pool.PooledConnection tomcatPooledConnection = tomcatProxy.getConnection(); //if this doesn't work try getDelegateConnection!

Connection connection = tomcatPooledConnection.getConnection();

然后尝试


org.postgresql.jdbc4.Jdbc4Connection jdbc4conn = (org.postgresql.jdbc4.Jdbc4Connection) connection;
jdbc4conn.getLargeObjectAPI();

如果上述演员表失败,请尝试:


org.postgresql.ds.jdbc23.AbstractJdbc23PooledConnection psAbstractPooledConn = Proxy.getInvocationHandler(connection.getProxy().getConnection());

好吧,如果它不起作用,如果您可以提供有关这些其他对象及其属性的更多信息,那将非常有用,但这里的想法是获取使用JDK Proxies生成的调用处理程序以及Tomcat代理的连接