如何将 NewProxyConnection 类型转换为 AS400JDBCConnection
我是 Spring 新手。我在 Spring 中使用 ComboPooledDataSource 进行连接池。 我正在使用 AS400 进行连接。 我的问题是,当我使用此连接并尝试对其进行类型转换时 AS400JDBCConnection as400Conn = (AS400JDBCConnection)conn; 它给出了 ClassCastCastException,因为 ComboPooledDataSource 返回的连接对象是 NewProxyConnection 类型,我如何将其类型转换为 AS400JDBCConnection。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该转换为
AS400JDBCConnection
。所有相关方法都应可通过Connection
接口使用。您所处理的不是实际的 Connection 对象(AS400JDBCConnection),而是包装在它周围并管理对原始 Connection 对象的访问的代理对象。不同的代理机制在 了解 AOP 代理
更新回复评论:访问方法需要
AS400JDBCConnection.getServerJobIdentifier()
。然后你必须切换到 CGLib 代理(解释此处)。You are not supposed to cast to
AS400JDBCConnection
. All relevant methods should be available through theConnection
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).Spring 确实支持解包代理的 ComboPooledDataSource 对象。如果您使用 JdbcTemplate,则可以将 nativeJdbcExtractor 属性设置为适当的对象。因此,任何可以检索本机 Connection 甚至任何派生对象(如 ResultSet)的地方都将是本机的。
NativeJdbcExtractor 接口的 JavaDoc,提供受支持的类的列表。这可以帮助您决定哪种实现适合您的应用程序。
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.