I was go through many solutions and my problem was solved but after some time the connection is timeout or disconnected.After 2 3 days I got a solution that solve my problem.
many solution suggest to use autoReconnect=true but when I was go through the docs. I saw the following text in the source describing the autoReconnect parameter:
The use of this feature is not recommended, because it has side effects related to session state and data consistency
When I looked in the Hibernate code. The basic connection mechanism of Hibernate doesn’t support reconnecting, one has to use H3C0 connection pool (which itself didn't always support reconnecting).
But once one’s used H3C0, the default behavior seems to be that to process a request, if the connection is dead then the user sees and error - but at least it reconnects for the next request. I suppose one error is better than infinite errors, but still not as good as zero errors. It turns out one needs the optiontestConnectionOnCheckout- which the documentation doesn’t recommend because testing the connection before a request might lead to lower performance. Surely the software firstly has to work, only secondly does it have to work fast.
So, to summarize, to get a connection to “work” (which I define as including handling dropped connections by reconnecting without error): In “hibernate.cfg.xml”:
<!-- hibernate.cfg.xml -->
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">1800</property>
<property name="c3p0.max_statements">50</property>
<!-- no "connection.pool_size" entry! -->
Then create a file “c3p0.properties” which must be in the root of the classpath (i.e. no way to override it for particular parts of the application):
c3p0.properties
c3p0.testConnectionOnCheckout=true
If this solution don't work than there are more possible solutions:-
1. Add
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
Also dont forget to place the c3p0-0.9.1.jar in the classpath.
2. Instead of using that c3p0.properties file, couldn't you just use this property in your hibernate.cfg.xml:
<property name="hibernate.c3p0.validate">true</property>
Also checkout the last post on this page:
https://forum.hibernate.org/viewtopic.php?p=2399313
If all these not work than go [more][1] and read in detail
[1]: http://hibernatedb.blogspot.in/2009/05/automatic-reconnect-from-hibernate-to.html