开发者

Spring framework to pass Connection object to legacy code

I am using Spring JDBC 3.0.6. I also have legacy code which uses plain JDBC. There are methods in the legacy code which required java.sql.Connection object. I want to call this method from my Spring code. How can I pass the java.sql.Connection object?

If I take connection object from the datasource then I need to manage the return/release of this connection. Can I not just get the reference of a connection object which is in the transaction.

I am using annotation based configuration and aop based declar开发者_运维技巧ative transactions.


Use JdbcTemplate.execute(ConnectionCallback). The connection callback will have access to the connection which is automatically opened, closed and associated to the current transaction by Spring.


Use the DataSourceUtils.getConnection method, this will retreive the connection associated with the transaction. Use DataSourceUtils.releaseConnection to release it (a noop if the connection is the one associated with the Transaction.

If the legacy code has util classes to open / close connection - you can just modify that to use the DataSourceUtils functions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜