Hello I'm working on a project using Hibernate 4.2.3 and Mysql as database.
My dependecies are:
Code:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.3.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>4.2.3.Final</version>
</dependency>
and my hibernate configuration file is like:
Code:
<!-- <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> -->
<property name="hibernate.c3p0.min_size">7</property>
<property name="hibernate.c3p0.max_size">53</property>
<property name="hibernate.c3p0.timeout">28800</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">1000</property>
<property name="hibernate.c3p0.validate">true</property>
<property name="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<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/tickets</property>
<property name="hibernate.connection.username">tickets</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<!-- <property name="hbm2ddl.auto">create</property> -->
<mapping class="maec.dafe.tickets.dao.Organisme" />
<mapping class="maec.dafe.tickets.dao.User" />
<mapping class="maec.dafe.tickets.dao.Status" />
<mapping class="maec.dafe.tickets.dao.Comments" />
<mapping class="maec.dafe.tickets.dao.Priority" />
<mapping class="maec.dafe.tickets.dao.Files" />
<mapping class="maec.dafe.tickets.dao.Ticket" />
<mapping class="maec.dafe.tickets.dao.OrganismeParent" />
<mapping class="maec.dafe.tickets.dao.TicketActions" />
</session-factory>
The problem is that after 8 hours of inactivity ( Weekend for example ) the application lose connection with the database and i get a message like this:
Code:
Struts has detected an unhandled exception:
Messages:
Communications link failure during commit(). Transaction resolution unknown.
unable to commit against JDBC connection
commit failed
File: org/hibernate/engine/transaction/internal/jdbc/JdbcTransaction.java
Line number: 116
I looked into multiple solutions and c3p0 configuration and i applied it as you can see on the configuration above but the problem still remains .
I don't know if i made a mistake somewhere in my dao code but the idea is that i created a main class called CrudUtil that manage all crud operation of the application and here is the code:
Code for saving objects in the database:
Code:
public void saveObject(Object object) {
final Session session = HibernateUtil.getSessionFactory() .getCurrentSession();
try {
session.getTransaction().begin();
session.saveOrUpdate(object);
session.getTransaction().commit();
} finally {
try {
session.close();
} catch (Exception e) {}
}
}
And here is the code used to get objects from the database:
Code:
public Object getObject(int id, Class cl) throws ObjectNotFoundException {
Object object = null;
try {
final Session session = HibernateUtil.getSessionFactory()
.getCurrentSession();
// Transaction tx = session.beginTransaction();
session.getTransaction().begin();
object = (Object) session.load(cl, id);
session.getTransaction().commit();
// tx.commit();
} catch (HibernateException e) { // TODO Auto-generated catch block
System.out.println("NOT FOUND");
e.printStackTrace();
}
return object;
// tx.commit(); finally {
}
Can you please tell me where's my error and what i need to improve or correct in my code..
PS:
There's a dummy solution is that to increase the mysql timeout global variable to infinite; but that's not a REAL solution.
Thank you very much.
mysql hibernate java-ee