3

我有一个在 Tomcat 中运行的 Java EE 应用程序,它使用 Tomcat 连接池,通过 postgres 数据库,并且该应用程序使用 OpenJPA for 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

4

1 回答 1

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 代理生成的调用处理程序和来自 Tomcat 代理的连接

于 2013-07-01T06:21:47.067 回答