在我们的应用程序中,我们使用休眠配置了c3p0池化连接。用数组类型的输入调用存储过程
我必须通过删除所有包装器来获得基础连接。但是,在这种情况下,当我尝试获取C3P0ProxyConnection类时,总是会收到ClassCastException。
是否有一种简单的方法来调用带有数组等复杂输入的存储过程?
在下面的代码中,我打印了一些类加载器信息,以查看铸造类是否由不同的类加载器加载。
类加载器。但是Connection类的位置为null,因此必须由Bootstrap类加载器加载。这可能是问题吗?
我已经附上了来自连接会话工厂的实际连接对象的快照。
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)
最佳答案
因此,有几件事:$Proxy315
不是C3P0ProxyConnection
。它是由hibernate生成的动态代理,该代理环绕BorrowedConnectionProxy。嵌入其中的可能是C3P0ProxyConnection
。如果确实需要,可以使用静态方法BorrowedConnectionProxy.getWrappedConnection(...)
解开c3p0 NewProxyConnection
的内容。您还可以使用JDBC连接标准方法unwrap。
但是您可能不想执行任何此类操作。如今,数组方法是标准的JDBC,您无需解开本机Connection就可以使用它们。只需跳过所有的幻想(即跳过c3p0原始Connection操作资料)和call createArrayOf(...) directly on the Connection。
可能必须获取包装的Connection的唯一原因是,如果您使用的是非常旧的休眠版本,而动态代理实现的是Connection的旧版本。如果是这种情况,请在投射前尝试BorrowedConnectionProxy.getWrappedConnection(...)
。而且由于您要进行大量的打印调试,所以请打印得到的Connection以确保它符合您的期望。