title: Extracting postgres connection object from tomcat connection pool and JPA
body: <p>I have a Java EE app that runs in Tomcat, uses Tomcat connection pooling, over postgres database, and the application uses OpenJPA for ORM.</p>
  
  <p>For some operations, I need to get access to LargeObjectManager API from postgres. That requires me having a reference to the actual postgres connection object.</p>
  
  <p>So, I'm trying to drill into the connection object I get from OpenJPA, to get the actual Postgres one. But I'm stopped by a 'sun.misc.proxy.$ProxyNN' object, and I'm not sure how can I get further.</p>
  
  <p>Here is what I'm doing now:</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>And that's where I stop. The returned object is below. I can't call ProxyConnection methods on it, and I don't even know who creates these proxies (but I bet it's Tomcat DBCP).</p>
  
  <pre><code>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
  </code></pre>
  
  <p>tomcat: 7.0.22
  postgres jdbc:9.2-1002
  openJPA: 2.1.1</p>