问题
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