These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Keep running out of connections - configuration problem?
PostPosted: Wed May 21, 2008 6:56 am 
Newbie

Joined: Wed May 21, 2008 6:38 am
Posts: 2
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 22, 2008 12:56 pm 
Newbie

Joined: Wed May 21, 2008 6:38 am
Posts: 2
One problem was my confusion over configuration options. When using the entity manger there is no need to have the Listener. So when I was getting an EntityManager it was being configured via the persistence.xml so any options were being ignored.

by adding this to persistence.xml it will use the hibernate.cfg.xml

Code:
<property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"/>


I am still eating connections but at least I am configuring the connection source I am actually using now ;0)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.