系统上线,急急急! Hibernate 非常奇怪的问题 org.hibernate.TransactionException: JDBC rollback failed

时间:2021-07-14 10:45:19
现象:
系统刚刚部署好的当天,访问几个系统没有问题,到了第二天访问该系统便会报错:org.hibernate.TransactionException: JDBC rollback failed,这个时候再重启tomcat一切又都好用了。也可以通过下面的方法测试,重新启动tomcat,一切好用,更改服务器的日期为第二天,结果几个系统就开始报错了。

            网上的解决方法如下:
方法一:上网查了一下,有人说是数据库事务的原因,要把hibernate的autocommit设为false,我找了一下这个好像要在代码里面设置,而不能通过简单的设置Hibernate的配置文件来设置,就没有继续走这条路。后来想到可能是数据库连接池的原因。
方法二:在hibernate配置文件中加入对c3p0连接池的配置,同时将c3p0-0.8.4.5.jar拷贝到应用的lib文件夹下。这里说明一下,如果没有这个配置,hibernate默认使用其自身的数据库连接池。配置后则采用第三方的c3p0连接池。
    使用c3p0连接池后确实没有问题了,但是我还是不明白为什么使用默认的连接池会有问题,如果哪位大哥清楚麻烦回复一下啊,谢谢!

代码如下:
 public boolean login(String userName, String password) throws Exception {
        Session session = HibernateUtil.currentSession();
        Transaction tx = null;
        ArrayList userslist = null;
        //String isActiva=null;

        try {
            tx = session.beginTransaction();
            Query query = session.createQuery(
                    "from SysUser as user where user.userName=:userName and user.userPassword=:userPassword and user.isActivation=1");
            query.setString("userName", userName);
            query.setString("userPassword", password);
            userslist = (ArrayList) query.list();
            
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                //tx.rollback();
            }
            throw e;
        } finally {
            tx = null;
            HibernateUtil.closeSession();
        }
        if (userslist == null || userslist.size() == 0) {
            return false;
       
        } else {
            return true;
        }
    }

请问高手,有没有更好的方法,以上解决的方法有效吗?现在系统上线,急急急! 

8 个解决方案

#1


配置如下:hibernate3.0,mysql

#2


tomcat,报错的异常如下:
2008-01-07 09:17:25 StandardWrapperValve[action]: Servlet.service() for servlet action threw exception
org.hibernate.TransactionException: JDBC rollback failed
at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:170)
at com.jandar.info.dao.SysUserDAO.login(SysUserDAO.java:250)
at com.jandar.info.actions.LoginAction.execute(LoginAction.java:49)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at com.jandar.info.util.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:106)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Caused by: com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Connection.close() has already been called. Invalid operation in this state.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:888)
at com.mysql.jdbc.Connection.getMutex(Connection.java:3588)
at com.mysql.jdbc.Connection.rollback(Connection.java:5035)
at org.hibernate.transaction.JDBCTransaction.rollbackAndResetAutoCommit(JDBCTransaction.java:183)
at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:162)
... 35 more

#3


session 没有关闭链接,
你自己找找,应该是这个问题

#4


查询不用使用事务

#5


public   boolean   login(String   userName,   String   password)   throws   Exception   { 
                Session   session   =   HibernateUtil.currentSession(); 
                Transaction   tx   =   null; 
                ArrayList   userslist   =   null; 
                //String   isActiva=null; 

                try   { 
                        tx   =   session.beginTransaction(); 
                        Query   query   =   session.createQuery( 
                                        "from   SysUser   as   user   where   user.userName=:userName   and   user.userPassword=:userPassword   and   user.isActivation=1"); 
                        query.setString("userName",   userName); 
                        query.setString("userPassword",   password); 
                        userslist   =   (ArrayList)   query.list(); 
                        
                        tx.commit(); 
                }   catch   (Exception   e)   { 
                        if   (tx   !=   null)   { 
                                tx.rollback(); 
                        } 
                        throw   e; 
                }   finally   { 
                        tx   =   null; 
                        HibernateUtil.closeSession(); //关闭session
                } 
                if   (userslist   ==   null   ¦ ¦   userslist.size()   ==   0)   { 
                        return   false; 
              
                }   else   { 
                        return   true; 
                } 
        } 

session我已经关闭连接了呀

#6


唉 到底要怎么整哦?

#7


靠。。。我很需要答案啊。。。。楼主怎么解决的啊

#8


楼主怎么配置的,能否透露一下下啊

#1


配置如下:hibernate3.0,mysql

#2


tomcat,报错的异常如下:
2008-01-07 09:17:25 StandardWrapperValve[action]: Servlet.service() for servlet action threw exception
org.hibernate.TransactionException: JDBC rollback failed
at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:170)
at com.jandar.info.dao.SysUserDAO.login(SysUserDAO.java:250)
at com.jandar.info.actions.LoginAction.execute(LoginAction.java:49)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at com.jandar.info.util.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:106)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Caused by: com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Connection.close() has already been called. Invalid operation in this state.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:888)
at com.mysql.jdbc.Connection.getMutex(Connection.java:3588)
at com.mysql.jdbc.Connection.rollback(Connection.java:5035)
at org.hibernate.transaction.JDBCTransaction.rollbackAndResetAutoCommit(JDBCTransaction.java:183)
at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:162)
... 35 more

#3


session 没有关闭链接,
你自己找找,应该是这个问题

#4


查询不用使用事务

#5


public   boolean   login(String   userName,   String   password)   throws   Exception   { 
                Session   session   =   HibernateUtil.currentSession(); 
                Transaction   tx   =   null; 
                ArrayList   userslist   =   null; 
                //String   isActiva=null; 

                try   { 
                        tx   =   session.beginTransaction(); 
                        Query   query   =   session.createQuery( 
                                        "from   SysUser   as   user   where   user.userName=:userName   and   user.userPassword=:userPassword   and   user.isActivation=1"); 
                        query.setString("userName",   userName); 
                        query.setString("userPassword",   password); 
                        userslist   =   (ArrayList)   query.list(); 
                        
                        tx.commit(); 
                }   catch   (Exception   e)   { 
                        if   (tx   !=   null)   { 
                                tx.rollback(); 
                        } 
                        throw   e; 
                }   finally   { 
                        tx   =   null; 
                        HibernateUtil.closeSession(); //关闭session
                } 
                if   (userslist   ==   null   ¦ ¦   userslist.size()   ==   0)   { 
                        return   false; 
              
                }   else   { 
                        return   true; 
                } 
        } 

session我已经关闭连接了呀

#6


唉 到底要怎么整哦?

#7


靠。。。我很需要答案啊。。。。楼主怎么解决的啊

#8


楼主怎么配置的,能否透露一下下啊