9
I have a server where I have several web applications connected to the database(Mysql
). The problem is that if any system gets idle it loses the connection to the bank and I have to refresh the application so that everything goes back to normal.
I was working around this problem with a Thread
that was ping the database every 1 hour. But now that I’m working with WEBServices
this method is no longer working.
I read in some places that the Mysql
drops all connections if idle for 8 hours. And I also found several possible solutions, like setting up the C3P0
, etc. But even doing these settings the error still persists. In the Tomcat
have these mistakes:
Caused by: org.hibernate.TransactionException: commit failed
Caused by: org.hibernate.TransactionException: unable to commit against JDBC connection
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
Caused by: java.net.SocketException: Software caused connection abort: socket write error
I’m using Hibernate
, and my settings are that way:
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/bd</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.autoReconnect">true</property>
<property name="connection.autoReconnectForPools">true</property>
<!-- C3P0 -->
<property name="c3p0.acquire_increment">5</property>
<property name="c3p0.timeout">0</property>
<property name="c3p0.min_size">3</property>
<property name="c3p0.max_size">100</property>
<property name="c3p0.max_statements">0</property>
<property name="c3p0.idle_test_period">3000</property>
I even put the timeout
as 0 because in some places they say that this way the connection never expires, but continues to expire.
Is my configuration right? Is it necessary to do some other configuration? Is there another way to solve this problem?
Updated
I realized that even with these errors I can still include, change and edit the data but every time I try to perform one of these operations I have as return error 500 and the screen is frozen, that is to say the fields are not cleared.
Updated 23-12-2015
Example of a method where I make the connection:
public void salvar(Agenda agenda) {
Session sessao = HibernateUtil.getSessionFactory().openSession();
Transaction transacao = null;
try {
transacao = sessao.beginTransaction();
sessao.save(agenda);
transacao.commit();
} catch (RuntimeException ex) {
ex.printStackTrace();
throw ex;
} finally {
sessao.close();
}
}
All methods of DAO
has the finally
with the sessao.close()
.
I was thinking, I have this property on hibernate.cfg
:
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
Could this interfere with the properties of C3P0
that I put?
I tried this but did not restart the service
Mysql
I will try again, in case I pass this value of 180 same?– DiegoAugusto
This link explains in detail how to solve this problem. http://forums.mysql.com/read.php?39,139821,160175
– Jorge Campos
@Techies http://stackoverflow.com/questions/3511422/java-c3p0-how-can-i-configure-autoreconnect-true Forget that comment, sorry, it won’t solve my problem. This link is from a problem equal to yours. And is taught how to solve this.
– adelmo00
I will take the test. Thank you. Now I have to wait for the system to get idle kk
– DiegoAugusto
I did the tests but the connection between the API and the bank keeps falling, and the strange thing is that the queries and the operations of delete, save edit are made but error 500. At first it doesn’t feel like you’ve been saved
– DiegoAugusto
It turns out some of your routines might be closing the connection. Also put how you are accessing the bank
– Emir Marques
See this example http://docs.jboss.org/hibernate/core/3.3/reference/en/html/tutorial.html#tutorial-tappfirs-helpers
– Emir Marques
I’ll test and see if it works
– DiegoAugusto
I already had Hibernateutil implemented, I did some more tests and it didn’t work
– DiegoAugusto
I think the key to understanding your problem lies in the way you open the connection in your application. Do you start and end when a connection? Is it always open? If so, why? Can you post code snippets where you open and close the connection?
– rodrigogq
I updated the question
– DiegoAugusto