Hi,
I am using JPA/Hibernate to create a lib that I am using in .sar file deploying it in sailfin. all this works fine, until I have recently got the exception of "Too many connections", MySQL database is screaming of too many connections. I am giving some more details below so I can be able to identify whether it's my code in my library that is failing using Hibernate or is the sailfin connection pool that is not handling the connections how they should.
I am using a JDBC connection pool, which I have specified in persistence.xml file and that looks like:
<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
http://java.sun.com/xml/ns/persistence/ ... ce_1_0.xsd"
version="1.0">
<persistence-unit name="hibernatetest">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>jdbc/mysql-resource</non-jta-data-source>
<class>com.research.data.User</class>
<class>com.research.data.User_Sessions</class>
<class>com.research.data.Devices</class>
<properties>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<!-- <property name="hibernate.hbm2ddl.auto" value="create-drop"/> -->
</properties>
</persistence-unit>
</persistence>
When it comes to JPA/Hibernate that is how I manage EntityManager/Factory:
public class DaoImpl {
private EntityManagerFactory emf = null;
private EntityManager em = null;
private EntityTransaction entityTransaction = null;
public DaoImpl() {
emf = Persistence.createEntityManagerFactory("hibernatetest");
em = emf.createEntityManager();
}
Then I have an open() method which is called before any created queries to the database:
public void open(){
if(!em.isOpen()){
emf = Persistence.createEntityManagerFactory("hibernatetest");
em = emf.createEntityManager();
System.out.println("OPEN!");
}
}
And when I do persist any data in the database the code looks like this:
public void saveUser(User user) {
try {
entityTransaction = em.getTransaction();
entityTransaction.begin();
em.persist(user);
entityTransaction.commit();
em.close();
} catch (RuntimeException e) {
e.printStackTrace();
if(entityTransaction != null){
entityTransaction.rollback();
}
}finally{
em.close();
}
}
The I have a close() function which is called when the application is undeployed:
public void close() {
em.close();
emf.close();
}
Has anyone considered such case or seen this trouble before?
Thanks!!!
Tux