开发者

Reload web server with gwt and c3p0 connection pool?

开发者 https://www.devze.com 2023-04-12 04:25 出处:网络
I have a web application written in gwt, and I\'m using a PostgreSQL database in the back end. When I make a new session on the server, I set up c3p0 and get a jdbc connection:

I have a web application written in gwt, and I'm using a PostgreSQL database in the back end. When I make a new session on the server, I set up c3p0 and get a jdbc connection:

ComboPooledDataSource source = new ComboPooledDataSource();
Properties connectionProps = new Properties();
connectionProps.put("user", "username");
connectionProps.put("password", "password");   
source.setProperties(connectionPro开发者_如何学Pythonps);
source.setJdbcUrl("some jdbc url that works");

and when I close my session on the server, I close the 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:

WARNING: A C3P0Registry mbean is already registered. This probably means that an application using c3p0 was undeployed, but not all PooledDataSources were closed prior to undeployment. This may lead to resource leaks over time. Please take care to close all PooledDataSources.

Which I assume means that reloading the web server didn't close the ComboPooledDataSource I made (probably a safe assumption). Is there any way I can get it to do that so I can obtain a connection after reloading the web server?


Closing dataSource generally (not only C3P0) is unadvisable because they should to be used from many applications on your server. 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.

Anyway if you neet to ged rid of the warning in your GWT console use this method in your 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);
        }
    }
}

}

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号