I am running tomcat 6 and mysql 5 and keep running out of connections while executing a particular piece of code.
EDIT: c3p0-0.9.1.2 and mysql-connector 5.0.4
I think I have read too much documentation and have confused myself about the startup and which settings will get used as a default.
The code is as follows - each time the method is called is deals with 30 Timesheet entities. The actual code does work fine (I have proved this by limiting the collection size to a smaller number)
Code:
public void storeWps() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("example");
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
for (Timesheet t:currentDates) {
if (!t.isTemp()) {
Workpackage wp = em.find(Workpackage.class, t.getTmpWorkpackageId());
Timesheet ts = em.merge(t);
if (ts == null) {
t.setWorkpackage(wp);
em.persist(t);
} else {
ts.setWorkpackage(wp);
}
}
}
em.getTransaction().commit();
} catch(Exception ex) {
logger.error("Error storing wps", ex);
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
} finally {
em.close();
}
}
My persistence.xml - (RESOURCE_LOCAL context is supplied)
Code:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd" version="1.0">
<persistence-unit name="example" transaction-type="RESOURCE_LOCAL">
<!-- Provider class name is required in Java SE -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
..classes..
<!-- All persistence classes must be listed -->
<properties>
<!-- Provider-specific connection properties -->
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/intranet"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="******"/>
<property name="hibernate.hbm2ddl.auto" value="none" /><!-- other values are: drop-and-create-tables|none -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
</properties>
</persistence-unit>
</persistence>
My hibernate.config.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">*******</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/intranet</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">intranet</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.jdbc.batch_size">30</property>
<property name="hibernate.c3p0.timeout">100</property>
<!-- Use the C3P0 connection pool. -->
<property name="c3p0.min_size">0</property>
<property name="c3p0.max_size">50</property>
<property name="c3p0.timeout">100</property>
<property name="hibernate.c3p0.max_statements">30</property>
<!-- Disable second-level cache. -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="cache.use_query_cache">false</property>
<property name="cache.use_minimal_puts">false</property>
<property name="max_fetch_depth">3</property>
<!-- Print SQL to stdout. -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- Drop and then re-create schema on SessionFactory build, for testing. -->
<property name="hbm2ddl.auto">none</property>
<!-- Bind the getCurrentSession() method to the thread. -->
<property name="current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
I am using the listener configuration method as documented
web.xml snippet
Code:
<listener>
<listener-class>foopackage.listener.HibernateListener</listener-class>
</listener>
The relevant code snippet inits hibernate with (assumption) the hibernate.config.xml as follows
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
My mysql hard limit is 100 connections which get exhausted after 3 submits of the method above. I can only assume that the connection pooling (c3p0) that I specified in the configuration does not work.
Could someone confirm that when in my code I request a new persistence context I am actually getting the context configured with the backing pool and settings as defined in hibernate.config.xml not one of the other files or even just some random defaults. My understanding is that the listener method uses hibernate.config.xml (it is in the class path).
It occurred to me that the persistence.xml may be being used however changing that had no effect on the connection loss either.