java - 每次服务器关闭时连接都会关闭。已添加超时 10000000。

标签 java tomcat connection-pooling servletcontextlistener

我已经使用 ContextListener 创建了连接池,并将这个项目托管到 tomcat.my 配置部分,如下所示。

在 tomcat config context.xml 中,我定义了如下资源。

<Resource name="jdbc/TEST_DS"
    auth="Container"
    type="javax.sql.DataSource"
    driverClassName="oracle.jdbc.driver.OracleDriver"
    url="jdbc:oracle:thin:@server:1521/db"
    username="uname"
    password="pwd"
    maxPoolSize="50"
    removeAbandoned="true"
    removeAbandonedTimeout="1000"
    logAbandoned="true"
/>

现在在 ContextListener 中使用此资源,如下所示。

public class ConnectionListener implements ServletContextListener {
    private DataSource dataSourceOracle = null;
    private Connection connectionOracle = null;
    private static final String ATTRIBUTE_NAME = "config";

    public void contextDestroyed(ServletContextEvent sce) {

        try {

            if(connectionOracle!=null && !connectionOracle.isClosed() ){
                            this.connectionOracle.close();
                            this.connectionOracle = null;
                    }
                ApplicationUtil.setServletContext(sce.getServletContext()); 

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
                e.printStackTrace();
                }
    }

    public void contextInitialized(ServletContextEvent event) {
            ServletContext servletContext = event.getServletContext();
            try {

                    String oracleDsName = servletContext.getInitParameter("oracle.ds.name");

                Context ctx = new InitialContext();
                        Context envContext = (Context) ctx.lookup("java:/comp/env");

                dataSourceOracle = (DataSource) envContext.lookup (oracleDsName); 
                connectionOracle =  dataSourceOracle.getConnection();
                        System.out.println("testing Oracle connection >> "+connectionOracle);

             ApplicationUtil.setServletContext(event.getServletContext()); 

            } catch (SQLException e) {
                        e.printStackTrace();
                }
                catch (NamingException e) {
                       e.printStackTrace();
               } catch (Exception e) {
                       e.printStackTrace();
               }

        servletContext.setAttribute(ATTRIBUTE_NAME, this);
    }

    public Connection getOracleConnection() throws SQLException, ClassNotFoundException {
       return this.connectionOracle;
        }

    public static ConnectionListener getInstance(ServletContext servletContext) {
            return (ConnectionListener) servletContext.getAttribute(ATTRIBUTE_NAME);
    }

}

现在使用方法调用此连接:

public class ApplicationUtil {
     private static ServletContext context;  
        /* Called by Listener */  
        public static void setServletContext(ServletContext context){  
            ApplicationUtil.context = context;  
        }  
        /* Use this method to access context from any location */  
        public static ServletContext getServletContext(){  
            return ApplicationUtil.context;  
        }  
}

public class DBAccess {
    ServletContext context = null;

    public DBAccess(ServletContext cnt) {
        context = cnt;
    }

    public Connection getOracleConnection() throws SQLException, ClassNotFoundException {
      return ConnectionListener.getInstance(context).getOracleConnection();
    }

    public List getLanguageList() {
      Connection cn = getOracleConnection();
      ...
   }

 }

这些都是我创建的连接池。现在的问题是当服务器关闭时连接将被关闭。而且我每次都需要重启tomcat来重新创建连接池。

有没有永久性的解决方案来解决这个问题??

如有任何建议,我们将不胜感激。

感谢副词。

最佳答案

数据库连接无法在数据库中存活。当数据库关闭时,连接丢失。
不要在上下文初始化中获取和存储连接对象。
每次需要时向池请求连接,并在处理后释放它。 如果有时数据库未启动,您会收到必须处理的错误,但如果数据库重新启动,您无需重新启动 tomat 即可获得连接。

关于java - 每次服务器关闭时连接都会关闭。已添加超时 10000000。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27478957/

相关文章:

java - getDocument() 不断返回空值

java - 删除文本文件中的最后一行

JavaPreparedStatements isClosed 性能

java - @Post 在 Rest 服务中接受 Json 值

java - BasicDataSource 有时会抛出 "Protocol not supported, abandoning connection."

java - java聊天服务器中的连接池

java - Tomcat无法读取我的context.xml文件

session 复制中的 Tomcat 集群

apache - 如何设置 Apache tomcat 和项目以显示 UTF-8 文本?

mysql - Django mysql 数据库连接池