gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:48:21 27 4
gpt4 key购买 nike

我有一个在 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.22postgres jdbc:9.2-1002openJPA: 2.1.1

最佳答案

试一试,可能需要一些调整,但我相信它应该可以正常工作,或者至少给你一些有用的指示,告诉你代理在哪里需要解包


//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 代理的连接

关于postgresql - 从tomcat连接池和JPA中提取postgres连接对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17394293/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com