DataSource is closed when application shuts down
This commit is contained in:
parent
54cff60c47
commit
8a67f06c02
1 changed files with 14 additions and 3 deletions
|
@ -23,6 +23,8 @@ import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContextEvent;
|
||||||
|
|
||||||
import org.apache.commons.dbutils.QueryRunner;
|
import org.apache.commons.dbutils.QueryRunner;
|
||||||
import org.apache.tomcat.jdbc.pool.DataSource;
|
import org.apache.tomcat.jdbc.pool.DataSource;
|
||||||
import org.apache.tomcat.jdbc.pool.PoolProperties;
|
import org.apache.tomcat.jdbc.pool.PoolProperties;
|
||||||
|
@ -32,6 +34,7 @@ import org.slf4j.LoggerFactory;
|
||||||
public class EssayistServletContextListener extends GuiceServletContextListener {
|
public class EssayistServletContextListener extends GuiceServletContextListener {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(EssayistServletContextListener.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(EssayistServletContextListener.class);
|
||||||
|
private DataSource dataSource;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Injector getInjector() {
|
protected Injector getInjector() {
|
||||||
|
@ -39,8 +42,8 @@ public class EssayistServletContextListener extends GuiceServletContextListener
|
||||||
Properties defaultProperties = new Properties();
|
Properties defaultProperties = new Properties();
|
||||||
try {
|
try {
|
||||||
defaultProperties.load(getClass().getResourceAsStream("/essayist-defaults.properties"));
|
defaultProperties.load(getClass().getResourceAsStream("/essayist-defaults.properties"));
|
||||||
} catch (IOException e1) {
|
} catch (IOException e) {
|
||||||
throw Throwables.propagate(e1);
|
throw Throwables.propagate(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
Properties properties = new Properties(defaultProperties);
|
Properties properties = new Properties(defaultProperties);
|
||||||
|
@ -57,7 +60,7 @@ public class EssayistServletContextListener extends GuiceServletContextListener
|
||||||
poolProperties.setDriverClassName(properties.getProperty("db.driverClassName"));
|
poolProperties.setDriverClassName(properties.getProperty("db.driverClassName"));
|
||||||
poolProperties.setInitialSize(Integer.parseInt(properties.getProperty("db.initialSize")));
|
poolProperties.setInitialSize(Integer.parseInt(properties.getProperty("db.initialSize")));
|
||||||
|
|
||||||
DataSource dataSource = new DataSource(poolProperties);
|
dataSource = new DataSource(poolProperties);
|
||||||
|
|
||||||
Connection connection = null;
|
Connection connection = null;
|
||||||
try {
|
try {
|
||||||
|
@ -98,5 +101,13 @@ public class EssayistServletContextListener extends GuiceServletContextListener
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void contextDestroyed(ServletContextEvent servletContextEvent) {
|
||||||
|
super.contextDestroyed(servletContextEvent);
|
||||||
|
if (dataSource != null) {
|
||||||
|
dataSource.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue