问题
I've built an application (app1) that looks at and records certain fields in a database. This application shares the database with another application (app2) that requires a solitary connection to it when starting up but is fine to allow other connections to the DB once it (app2) is started. In my application (app1), I have made a dao object using Spring to connect to the DB and evidently, the connection is never closed which causes app2 to crash upon start up. From what I've read, Spring is supposed to automatically handle opening and closing all of the DB connections it manages. I'm not sure of any code I could share to help paint a better picture of my problem but if some is needed, I'll post what I can. Thanks for any help.
回答1:
If you're using the JDBC template you don't have to worry about explicitly closing connections, Spring will take care of internally managing a connection pool and obtaining/releasing connections from that pool.
回答2:
When sharing connections between applications I would recommend using a connection pool. Opening and closing of connections could be done by declarative transaction demarcation via annotations (@Transactional).
http://faheemsohail.com/2012/01/configuring-c3p0-connection-pooling-with-spring-and-hibernate/
回答3:
Would this work for you?
public void closeCon() {
{
if (con != null)
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
来源:https://stackoverflow.com/questions/13920861/how-to-close-a-jdbc-connection-created-through-spring