Use C3P0 for Hibernate connection pooling. This fixes the issue where everything breaks if Postgres restarts.

This commit is contained in:
Andy Janata 2018-02-15 16:27:53 -08:00
parent 88f58fb0fe
commit 91db059995
3 changed files with 28 additions and 10 deletions

View File

@ -282,6 +282,11 @@
<artifactId>hibernate-core</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>

View File

@ -18,6 +18,12 @@
<property name="show_sql">${hibernate.sql.show}</property>
<property name="format_sql">${hibernate.sql.format}</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">10</property>
<mapping class="net.socialgamer.cah.db.PyxBlackCard" />
<mapping class="net.socialgamer.cah.db.PyxWhiteCard" />
<mapping class="net.socialgamer.cah.db.PyxCardSet" />

View File

@ -1,5 +1,5 @@
/**
* Copyright (c) 2012-2017, Andy Janata
* Copyright (c) 2012-2018, Andy Janata
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
@ -37,13 +37,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.ServletContext;
import net.socialgamer.cah.data.GameManager;
import net.socialgamer.cah.data.GameManager.GameId;
import net.socialgamer.cah.data.GameManager.MaxGames;
import net.socialgamer.cah.data.User;
import net.socialgamer.cah.metrics.Metrics;
import net.socialgamer.cah.metrics.UniqueIds;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import com.google.inject.AbstractModule;
@ -52,6 +46,13 @@ import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
import com.google.inject.assistedinject.FactoryModuleBuilder;
import net.socialgamer.cah.data.GameManager;
import net.socialgamer.cah.data.GameManager.GameId;
import net.socialgamer.cah.data.GameManager.MaxGames;
import net.socialgamer.cah.data.User;
import net.socialgamer.cah.metrics.Metrics;
import net.socialgamer.cah.metrics.UniqueIds;
/**
* CAH Guice module.
@ -60,7 +61,9 @@ import com.google.inject.assistedinject.FactoryModuleBuilder;
*/
public class CahModule extends AbstractModule {
private final static Properties properties = new Properties();
private static final Logger LOG = Logger.getLogger(CahModule.class);
private final Properties properties = new Properties();
private final ServletContext context;
@ -179,7 +182,11 @@ public class CahModule extends AbstractModule {
*/
@Provides
Session provideHibernateSession() {
return HibernateUtil.instance.sessionFactory.openSession();
final Session session = HibernateUtil.instance.sessionFactory.openSession();
if (!session.isConnected()) {
LOG.error("Session disconnected!");
}
return session;
}
@BindingAnnotation