Exception in beginTransaction
I am using hibernate in my web application,when I am getting exception while:
Transaction tx=session.beginTransaction();
It gives exception once in a while, I am reloading SessionFactory
while catching the exception and then it runs fine.
It is working when I am reloading the SessionFactory again.
This is my code snippet:
session = HibernateFactory.openSession(0);//Passing 0 uses the existing sessionFactory
if(session.isConnected() && session.isOpen() && session!=null)
{
try{
tx = session.beginTransaction();
logger.info("Transaction is begin successfully--Transaction is--"+tx);
}catch(Exception e){
logger.info("Could not start the transaction--Transaction is--"+tx);
session = HibernateFactory.openSession(1);//passing 1 is creating new //--SessionFactory
tx = session.beginTransaction();//now this runs fine
logger.info("Inside catch--Obtained session--"+session+"and transaction--"+tx);
e.printStackTrace();
logger.error("e.getCause(): "+e.getCause());
logger.error("e.getClass(): "+e.getClass());
// throw new HibernateException(e.getMessage());
}
}
This is the stackTrace of the exception I am getting.
org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:426)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
at com.arosys.hibernateDao.AbstractDao.startOperation(AbstractDao.java:184)
at com.arosys.hibernateDao.AbstractDao.findAll(AbstractDao.java:150)
at com.arosys.beansDao.JobHierarchyDao.findAll(JobHierarchyDao.java:20)
at org.ArosysLogin.server.GWTJobImpl.getJobHierarchy(GWTJobImpl.java:416)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:569)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:开发者_如何学Go127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584)
at java.lang.Thread.run(Thread.java:662)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 0 ms ago.
Just guessing since there isn't enough information (type and message of the exception):
Connection fails due to flaky database/network connection
do you commit your transactions? I'm not sure, but Hibernate might complain if you try to start a transaction twice.
UPDATE based on the call stack.
This seems to be a problem with the database. check out the various results for googling "com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure"
This one might help: https://serverfault.com/questions/89955/unable-to-connect-to-mysql-through-jdbc-connector-through-tomcat-or-externally
Are you sure your connections get closed after usage? I can imagine the database denying access after so many connections are opened. Checking the logs of your database might help as well.
- Set
ValidateConnectionOnBorrow
totrue
- and add a validation query for
SQLForValidateConnection
- Set
autoReconnect
to true for jdbc URL
either by Server connection pool configuration or connection pool configuration.
I assume(from the stack trace) that you are using MySQL,Hibernate with either JBoss or Tomcat.
Let me know in case you face any problem, I faced the same long ago.
精彩评论