gpt4 book ai didi

java - 将 NewProxyConnection 转换为 C3P0ProxyConnection 时出现 ClassCastException

转载 作者:行者123 更新时间:2023-12-02 12:15:36 33 4
gpt4 key购买 nike

在我们的应用程序中,我们使用 hibernate 配置了 c3p0 池连接。使用数组类型的输入调用存储过程 我必须通过删除所有包装来获得底层连接。但是在这种情况下,当我尝试获取 C3P0ProxyConnection 类时,我总是收到 ClassCastException。

是否有一种简单的方法来调用具有复杂输入(如数组)的存储过程? 在下面的代码中,我打印了一些类加载器信息,以查看强制转换类是否由不同的类加载器加载。但似乎所有类加载器都由同一个类加载器加载 类加载器。但Connection类位置为空,因此必须由Bootstrap类加载器加载。这可能是问题所在吗?

我已附加来自 hinernate session 工厂的实际连接对象的快照。

private List<InvoicePrintDetailsDTO> getSelectedInvoiceDetailsToPrint(List<Integer> selectedInvoiceIds) {
Currency defaultCurrency = DAOInstanceFactory.getInstance().getCurrencyDAO().loadCurrencyByCurrencyCode("LKR");
List<InvoicePrintDetailsDTO> listOfInvoicesToPrint = Collections.emptyList();
Session session = DAOInstanceFactory.getInstance().getSessionFactory().getCurrentSession();
Connection connection = session.connection();

try {
Object[] invoiceIds = selectedInvoiceIds.toArray();
Array invoiceIdsARRAY = getSqlArray(invoiceIds,"SELECTEDINVOICEIDS",connection);

String query = "{call GETINVOICEPRINTDATA(?,?,?,?)}";
CallableStatement ocs = connection.prepareCall(query);
ocs.setArray(1, invoiceIdsARRAY);
ocs.setInt(2, defaultCurrency != null ? defaultCurrency.getId() : 0);
ocs.setInt(3, 1000);
ocs.registerOutParameter(4, OracleTypes.ARRAY,"INVOICEPRINTINVOICES");
ocs.executeUpdate();

Array resultsAsArray = ocs.getArray(8);
Map<String,Class<?>> resultsAsMap = connection.getTypeMap();
resultsAsMap.put("INVOICEPRINTINVOICEINFO", Class.forName("com.xxxx.xxxxx.business.utill.InvoicePrintDetailsDTO"));

if(resultsAsArray!=null) {
Object[] values = (Object[]) resultsAsArray.getArray();

if(values != null && values.length > 0) {
listOfInvoicesToPrint = new ArrayList<InvoicePrintDetailsDTO>();
for (int i=0; i < values.length; i++){
InvoicePrintDetailsDTO obj = (InvoicePrintDetailsDTO)values[i];
listOfInvoicesToPrint.add(obj);
}
}
}
} catch (Exception exception) {
logger.error(ERROR_OCCOURED_WHILE_GENERATING_INVOICE_PRINT, exception);
} finally {
try {
if(connection != null && !connection.isClosed()){
connection.close();
}
} catch (SQLException sqlException) {
logger.error(ERROR_OCCOURED_WHILE_GENERATING_INVOICE_PRINT, sqlException);
}
}
return listOfInvoicesToPrint;
}

private Array getSqlArray(Object[] data, String sqlTypeName, Connection connection) throws SQLException {
Array array = null;

Class<? extends Connection> connectionClass = connection.getClass();
ClassLoader connectionClassLoader = connectionClass.getClassLoader();
System.out.println("Connection class loader is :"+connectionClassLoader.toString());
System.out.println("Connection class location :"+connectionClass.getResource('/' + connectionClass.getName().replace('.', '/') + ".class"));

Class<C3P0ProxyConnection> c3P0ProxyConnectionClass = C3P0ProxyConnection.class;
ClassLoader proxyConnectionLoader = c3P0ProxyConnectionClass.getClassLoader();
System.out.println("Proxy Connection class loader is :"+proxyConnectionLoader.toString());
System.out.println("C3P0ProxyConnection class location :"+c3P0ProxyConnectionClass.getResource('/' + c3P0ProxyConnectionClass.getName().replace('.', '/') + ".class"));

Class<NewProxyConnection> newProxyConnectionClass = NewProxyConnection.class;
ClassLoader newProxyConnectionLoader = newProxyConnectionClass.getClassLoader();
System.out.println("New Proxy Connection class loader is :"+newProxyConnectionLoader.toString());
System.out.println("NewProxyConnection class location :"+newProxyConnectionClass.getResource('/' + newProxyConnectionClass.getName().replace('.', '/') + ".class"));

C3P0ProxyConnection proxyValue = (C3P0ProxyConnection) connection;

if (connection instanceof C3P0ProxyConnection) {
C3P0ProxyConnection proxy = (C3P0ProxyConnection) connection;
try {
Method m = Connection.class.getMethod("createArrayOf", String.class, Object[].class);
Object[] arrayOfArguments = { sqlTypeName, data };
array = (Array) proxy.rawConnectionOperation(m, C3P0ProxyConnection.RAW_CONNECTION, arrayOfArguments);
} catch (Exception exception) {
logger.error("Error calling createArrayOf", exception);
throw new SQLException(exception);
}
}
return array;
}

错误日志

20:36:53,622 INFO  [STDOUT] Connection class loader is :WebappClassLoader
delegate: false
repositories:
/WEB-INF/classes/
----------> Parent Classloader:
java.net.FactoryURLClassLoader@21bdbf51
20:36:53,623 INFO [STDOUT] Connection class location :null
20:36:53,624 INFO [STDOUT] Proxy Connection class loader is :WebappClassLoader
delegate: false
repositories:
/WEB-INF/classes/
----------> Parent Classloader:
java.net.FactoryURLClassLoader@21bdbf51
20:36:53,624 INFO [STDOUT] C3P0ProxyConnection class location :jar:file:/D:/jboss-4.2.2.GA/server/default/deploy/xxxx.war/WEB-INF/lib/c3p0-0.9.5.2.jar!/com/mchange/v2/c3p0/C3P0ProxyConnection.class
20:36:53,625 INFO [STDOUT] New Proxy Connection class loader is :WebappClassLoader
delegate: false
repositories:
/WEB-INF/classes/
----------> Parent Classloader:
java.net.FactoryURLClassLoader@21bdbf51
20:36:53,626 INFO [STDOUT] NewProxyConnection class location :jar:file:/D:/jboss-4.2.2.GA/server/default/deploy/xxxx.war/WEB-INF/lib/c3p0-0.9.5.2.jar!/com/mchange/v2/c3p0/impl/NewProxyConnection.class
20:36:53,631 INFO [STDOUT] ERROR [http-10.10.10.117-8080-6] (InvoicePDFBody.java:316) - Error occoured while generating invoice print.
java.lang.ClassCastException: $Proxy315 cannot be cast to com.mchange.v2.c3p0.C3P0ProxyConnection
at com.xxxx.xxxx.print.handlers.InvoicePDFBody.getSqlArray(InvoicePDFBody.java:358)
at com.xxxx.xxxx.print.handlers.InvoicePDFBody.getSelectedInvoiceDetailsToPrint(InvoicePDFBody.java:290)
at com.xxxx.xxxx.print.handlers.InvoicePDFBody.buildPdfDocument(InvoicePDFBody.java:413)
at com.xxxx.document.print.controller.InvoicePDFGenerator.buildPdfDocument(InvoicePDFGenerator.java:19)
at org.springframework.web.servlet.view.document.AbstractPdfView.renderMergedOutputModel(AbstractPdfView.java:78)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.extremecomponents.table.filter.AbstractExportFilter.doFilter(AbstractExportFilter.java:49)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.custom.spring.plugin.AuthenticationFilter.doFilter(AuthenticationFilter.java:39)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.custom.spring.plugin.RoleBasedAccessFilterForInviocePrint.doFilter(RoleBasedAccessFilterForInviocePrint.java:60)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.custom.spring.plugin.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:199)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
at java.lang.Thread.run(Thread.java:662)

enter image description here

最佳答案

所以,有几件事:

  1. $Proxy315 不是 C3P0ProxyConnection。它是由 hibernate 生成的动态代理,包装了 BorrowedConnectionProxy 。其中嵌入的可能是一个C3P0ProxyConnection。如果您确实愿意,可以使用静态方法 BorrowedConnectionProxy.getWrappedConnection(...) 来解包到可能是 c3p0 NewProxyConnection 的内容。您还可以使用 JDBC 连接标准方法 unwrap .
  2. 但您可能不想这样做。数组方法是当今标准的 JDBC,您不必解包到 native Connection 即可使用它们。只需跳过所有花哨的内容(即跳过 c3p0 原始连接操作内容),以及 call createArrayOf(...) directly on the Connection .

您可能必须获取包装的 Connection 的唯一原因是,如果您使用的是非常旧版本的 hibernate,并且动态代理实现了旧版本的 Connection。如果是这种情况,请在转换之前尝试 BorrowedConnectionProxy.getWrappedConnection(...) 。由于您正在进行大量的打印调试,因此请打印您获得的连接以确保它是您所期望的。

关于java - 将 NewProxyConnection 转换为 C3P0ProxyConnection 时出现 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46201465/

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