Connection to the Bank drops after a certain time

Asked

Viewed 3,664 times

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?

  • This link explains in detail how to solve this problem. http://forums.mysql.com/read.php?39,139821,160175

  • @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.

  • I will take the test. Thank you. Now I have to wait for the system to get idle kk

  • 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

  • It turns out some of your routines might be closing the connection. Also put how you are accessing the bank

  • See this example http://docs.jboss.org/hibernate/core/3.3/reference/en/html/tutorial.html#tutorial-tappfirs-helpers

  • I’ll test and see if it works

  • I already had Hibernateutil implemented, I did some more tests and it didn’t work

  • 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?

  • I updated the question

Show 6 more comments

2 answers

4


There are several things to try to test, I will put some.

On the connection line you can try to put the autoReconnect argument

<property name="connection.url">jdbc:mysql://localhost:3306/bd?autoReconnect=true</property>

Also add a preview for C3P0

<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>  

And finally, the property to test the connection at a checkout.

Create a file c3p0.properties, or if you are already using the file c3p0-config.xml add the property as true http://www.mchange.com/projects/c3p0/index.html#testConnectionOnCheckout

A second alternative

Is to find the file my.ini or my.cnf in the directory of mysql respective to your operating system, in that link there are examples of paths where the directory is.

If you do not initially find it, also check that the file is not found occult. In either of them it works.

When finding, add the following lines with the values, which are recognized in seconds

wait_timeout = 99999999999999999999999 
interactive_timeout = 99999999999999999999999

By default these even implicit values are 8 horas, or 28800 segundos.

  • i have to create that c3p0.properties or I can do the config in the Hibernate file same?

  • When I put a provider have that mistake: Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]

  • You can test, but the line pattern is different from the arrows you set. Maybe it’s because you’re not using Ibernate’s c3p0, but the other available version? http://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 http://mvnrepository.com/artifact/com.mchange/c3p0 But both are likely to be necessary, but the change stays in the configuration.

  • Actually I did not have the dependency of Hibernate-c3p0, I thought it was already integrated. I will test again the Provider

  • The provider still not working. The rest you went through is working

  • But you can provoke the problem you are having, to test the settings?

  • So I always had to wait until the next day, because the system is on the server and the error only happens when it is inactive for a few hours.

  • And counted how many hours, it’s always the same amount?

  • According to what I read are 8 hours, which is the default of mysql

  • But then the problem is in Mysql configuration and not in your application, you changed and increased this timeout time?

  • Yes I increased, in the same place I saw that this could be solved through the connections pool Hibernate, not to let lose the connection with the bank.

  • No my.cnf? Maybe just change there, you wouldn’t need to add more dependencies to the project just to solve this problem

  • I already made this change, from a look at the Hibernate boot log, will the c3p0 is working: http://pastebin.com/B7XA1CRw

  • Hm, weird. You can show off like this my.cnf?

  • Yes, I can, 1 minute

  • For Hibernate C3P0 to initialize, Provider is required. If the error is happening, try switching to the properties of Hibernate c3p0 itself.

  • 1

    I added these two properties to the file my.ini and it worked.

Show 13 more comments

1

Take a look at the settings of this other post, I believe that with this your problem will be solved.

Watch out for the properties idleConnectionTestPeriod and maxIdleTimeExcessConnections

Best Configuration of c3p0 [closed]

  • Thank you, I’ll test

Browser other questions tagged

You are not signed in. Login or sign up in order to post.