我应该关闭JNDI获得的数据源吗? [英] Should I close JNDI-obtained data source?

查看:217
本文介绍了我应该关闭JNDI获得的数据源吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:显然,Tomcat从7.0.11开始,为您关闭了数据源,因此它在webapp的contextDestroyed中不可用。请参阅: https://issues.apache.org/bugzilla/show_bug.cgi?id=25060

Update: Apparently Tomcat, starting with 7.0.11, closes the DataSource for you, so it's not available in the webapp's contextDestroyed. See: https://issues.apache.org/bugzilla/show_bug.cgi?id=25060

我正在使用Spring 3.0和Java 1.6。

I'm using Spring 3.0 and Java 1.6.

如果我这样获得数据源:

If I get a 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>

然后在销毁bean时关闭数据源。

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

如果我得到这样的数据源:

If I get the data source like this:

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

那么我是否必须在上下文中明确关闭数据源销毁的侦听器?

then do I have to explicitly close the data source in my contextDestroyed listener?

谢谢

Paul

推荐答案

<我不同意。我将在您的web.xml中添加一个侦听器并实现contextDestroyed()方法。当Web应用程序被销毁或取消部署时,Web容器/应用程序服务器将调用此方法。在contextDestroyed()中,我将关闭数据源。

I disagree. I would add a listener to your web.xml and implement the contextDestroyed() method. This method will get called by your web container/app server when the web app is destroyed or undeployed. Within the contextDestroyed(), I would close the datasource.

在web.xml内部

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

代码:

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);
  }
}

这篇关于我应该关闭JNDI获得的数据源吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆