Should I close the data source obtained from JNDI?

Update. Apparently, Tomcat, starting from 7.0.11, closes the DataSource for you, so it is not available in the webappdestroyed context. See: https://issues.apache.org/bugzilla/show_bug.cgi?id=25060

Hi,

I am using Spring 3.0 and Java 1.6.

If I get the data source this way:

<bean id="dataSource" class="my.data.Source" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521:home"/> <property name="username" value="user"/> <property name="password" value="pw"/> </bean> 

then the data source is closed when the bean is destroyed.

If I get the data source as follows:

 <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/db" /> 

then do i need to explicitly close the data source in my context? Sophisticated listener?

Thanks,

Floor

+8
spring spring-jdbc tomcat7 datasource jndi
source share
3 answers

I do not agree. I would add a listener to your web.xml and implement the contextDestroyed () method. This method will be called by your web container / application server when the web application is destroyed or not deployed. Inside contextDestroyed (), I would close the data source.

inside web.xml

 <listener> <listener-class>util.myApplicationWatcher</listener-class> </listener> 

The code:

 package util; public class myApplicationWatcher implementes ServletContextListener { public void contextInitialized(ServletContextEvent cs) { // This web application is getting started // Initialize connection pool here by running a query JdbcTemplate jt = new JdbcTemplate(Dao.getDataSource() ); jt.queryForInt("Select count(col1) from some_table"); } public void contextDestroyed(ServeletContextEvent ce) { // This web application is getting undeployed or destroyed // close the connection pool Dao.closeDataSource(); } } public class Dao { private static DataSource ds; private static bDataSourceInitialized=false; private static void initializeDataSource() throws Exception { InitialContext initial = new InitialContext(); ds = (DataSource) initial.lookup(TOMCAT_JNDI_NAME); if (ds.getConnection() == null) { throw new RuntimeException("I failed to find the TOMCAT_JNDI_NAME"); } bDataSourceInitialized=true; } public static void closeDataSource() throws Exception { // Cast my DataSource class to a c3po connection pool class // since c3po is what I use in my context.xml ComboPooledDataSource cs = (ComboPooledDatasource) ds; // close this connection pool cs.close(); } public static DataSource getDataSource() throws Exception { if (bDataSourceInitialized==false) { initializeDataSource(); } return(ds); } } 
+5
source share

Not. DataSource here is managed by the remote JNDI container, and this is the job of the container to manage the DataSource life cycle. Spring just uses it; it does not control it.

Even if you want it, you cannot - the DataSource does not have a close() method or something like that.

+4
source share

When you get a data source through a JNDI search, this is a shared resource configured in your container. It is managed by the container, not by the application, so it is not required (no way) to close it.

+1
source share

All Articles