how can I typecast NewProxyConnection into AS400JDBCConnection

有些话、适合烂在心里 提交于 2019-12-25 03:57:11

问题


I am new to Spring . I am using ComboPooledDataSource for connection pooling in Spring. I am using the AS400 for making the connection. My problem is that when I am using this connection and try to typecast this AS400JDBCConnection as400Conn = (AS400JDBCConnection)conn; It gives the ClassCastCastException because the connection object returned by the ComboPooledDataSource is of type NewProxyConnection how can I typecast it into AS400JDBCConnection.


回答1:


Spring does support unwrapping the proxied ComboPooledDataSource object. If you are using JdbcTemplate, you can set the nativeJdbcExtractor property to an appropriate object. So any place you can retrieve a native Connection or even any of the derived objects (like ResultSet) will be native.

The JavaDoc for the NativeJdbcExtractor interface for a list of supported classes. That can help you decide which implementation works for you application.




回答2:


You are not supposed to cast to AS400JDBCConnection. All relevant methods should be available through the Connection interface.

What you are dealing with is not the actual Connection object (the AS400JDBCConnection), but a proxy Object that is wrapped around it and manages access to the original Connection Object. The different proxy mechanisms are explained in Understanding AOP Proxies

Update responding to comments: Access to the Method AS400JDBCConnection.getServerJobIdentifier() is needed. Then you will have to switch to CGLib proxying (explained here).



来源:https://stackoverflow.com/questions/7305317/how-can-i-typecast-newproxyconnection-into-as400jdbcconnection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!