简体   繁体   English

用gwt和c3p0连接池重新加载Web服务器?

[英]Reload web server with gwt and c3p0 connection pool?

I have a web application written in gwt, and I'm using a PostgreSQL database in the back end. 我有一个用gwt编写的Web应用程序,并且在后端使用了PostgreSQL数据库。 When I make a new session on the server, I set up c3p0 and get a jdbc connection: 当我在服务器上进行新会话时,我设置了c3p0并获得了jdbc连接:

ComboPooledDataSource source = new ComboPooledDataSource();
Properties connectionProps = new Properties();
connectionProps.put("user", "username");
connectionProps.put("password", "password");   
source.setProperties(connectionProps);
source.setJdbcUrl("some jdbc url that works");

and when I close my session on the server, I close the ComboPooledDataSource. 当我关闭服务器上的会话时,我关闭了ComboPooledDataSource。

However... when I press the yellow "reload web server" button in GWT development mode and refresh my page, I get the following warning, and a bunch of subsequent errors preventing me from obtaining a database connection: 但是...当我在GWT开发模式下按黄色的“重新加载Web服务器”按钮并刷新页面时,收到以下警告,以及许多后续错误,这些错误阻止我获得数据库连接:

WARNING: A C3P0Registry mbean is already registered. 警告:C3P0Registry mbean已被注册。 This probably means that an application using c3p0 was undeployed, but not all PooledDataSources were closed prior to undeployment. 这可能意味着已取消部署使用c3p0的应用程序,但并非所有PooledDataSources在取消部署之前都已关闭。 This may lead to resource leaks over time. 随着时间的流逝,这可能导致资源泄漏。 Please take care to close all PooledDataSources. 请注意关闭所有PooledDataSources。

Which I assume means that reloading the web server didn't close the ComboPooledDataSource I made (probably a safe assumption). 我认为这意味着重新加载Web服务器不会关闭我所做的ComboPooledDataSource(可能是一个安全的假设)。 Is there any way I can get it to do that so I can obtain a connection after reloading the web server? 有什么办法可以做到这一点,以便在重新加载Web服务器后可以获得连接?

Closing dataSource generally (not only C3P0) is unadvisable because they should to be used from many applications on your server. 通常不建议关闭dataSource(不仅是C3P0),因为应从服务器上的许多应用程序中使用它们。 If you kill this connection pool other can loose data access. 如果您杀死该连接池,其他连接池可能会丢失数据访问权限。 In practice you shoud leave pool management to your container and use only JNDI. 实际上,您应该将池管理留给您的容器,并且仅使用JNDI。

Anyway if you neet to ged rid of the warning in your GWT console use this method in your EventListener contextDestroyer: 无论如何,如果您需要在GWT控制台中消除警告,请在EventListener contextDestroyer中使用此方法:


public abstract class YourListenerimplements EventListener {
    //Probably you initialize your dataSource here. I do it with Guice.
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ...
    }

@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
    try {
        connection = dataSource.getConnection(); //Your dataSource (I obtain it from Guice)
    } catch (SQLException ex) {
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
            if (dataSource != null) {
                try {
                    DataSources.destroy(dataSource);
                    dataSource = null;
                } catch (Exception e) {
                }
            }
        } catch (SQLException sQLException) {
            XLog.error(sQLException);
        }
    }
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM