为什么不工作?使用Hibernate和Struts 2并获得异常java.lang.IllegalStateException:事务已处于活动状态

时间:2016-07-27 19:00:03

标签: java hibernate transactions

我是hibernate的新手,这是我使用hibernate和Struts2的第二个例子,JSP表单应该将数据存储在数据库中但得到以下异常。

发布异常的堆栈跟踪:

java.lang.IllegalStateException: Transaction already active
org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:52)
    com.InstaRecharge.hbm.UserDao.register(UserDao.java:16)
    com.InstaRecharge.action.Register.execute(Register.java:63)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:871)
    ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1294)
    ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
    com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:117)
    com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:108)
    ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1370)
    ognl.ASTMethod.getValueBody(ASTMethod.java:90)
    ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
    ognl.SimpleNode.getValue(SimpleNode.java:258)
    ognl.Ognl.getValue(Ognl.java:494)
    ognl.Ognl.getValue(Ognl.java:458)
    com.opensymphony.xwork2.ognl.OgnlUtil$2.execute(OgnlUtil.java:315)
    com.opensymphony.xwork2.ognl.OgnlUtil.compileAndExecute(OgnlUtil.java:346)
    com.opensymphony.xwork2.ognl.OgnlUtil.getValue(OgnlUtil.java:313)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:430)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:290)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:251)
    org.apache.struts2.interceptor.DeprecationInterceptor.intercept(DeprecationInterceptor.java:41)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:168)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:76)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    org.apache.struts2.interceptor.DateTextFieldInterceptor.intercept(DateTextFieldInterceptor.java:125)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:253)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:140)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:567)
    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)`

这是Register.java

import com.InstaRecharge.hbm.UserDao;
import com.opensymphony.xwork2.ActionSupport;
import java.sql.*;
public class Register extends ActionSupport {
    private String id;
    private String name;
    private String email;;
    private int phone;
    private String password;
    private String repassword;

    //getter setter methods


    public String execute() throws  SQLException{

        /* ****for oracle database**** 
        Class.forName("oracle.jdbc.driver.OracleDriver");


        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","SYSTEM","SYSTEM");


        ####MySql database###
        try{Class.forName("com.mysql.jdbc.Driver");}catch(Exception e){e.printStackTrace();}

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","system");
        PreparedStatement ps = con.prepareStatement("insert into userdetail values(?,?,?,?)");
        ps.setString(1, email);
        ps.setString(2, password);
        ps.setString(3, name);
        ps.setInt(4, phone);
        int i= ps.executeUpdate();*/

        int i=UserDao.register(this);
        if(i>0){
            return SUCCESS;


        }else{
            return ERROR;
        }
    }
    String blank="Cannot be blank!!";

    public void validate(){
        int e = email.length();
        int i = email.indexOf("@");
        int d =email.indexOf(".");

        if (name.length()<1){
            addFieldError("name", blank);
        }

        if(email.length()<1){
            addFieldError("email", blank);
        }else if(i>0 && i<(e-5) && d>0 && d< (i-1) ){
            //do nothing
        }else{
            addFieldError("email", "Invalid e-mail id!!!");

        }
        if( phone < 1 ){
            addFieldError("phone", blank);
        }
        if(password.length()<1){
            addFieldError("password", blank);
        }

        if(repassword.equals(password)){
        }else{
            addFieldError("repassword", "Password did not match!");

        }
    }



}

这是我的UserDao.java

import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class UserDao {
public static int register(Register u){
    int i=0;
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();
    Session session = factory.openSession();

    Transaction t = session.beginTransaction();
    t.begin();

    i=(Integer)session.save(u);
    t.commit();
    session.close();
    return i;
}
}

1 个答案:

答案 0 :(得分:2)

Nathan Hughes建议的代码存在一个基本缺陷,我只是要详细说明。

您尝试创建的会话是在sessionfactory的帮助下完成的。现在,由于您每次都尝试创建sessionfactory,因此线程可能会访问已由先前线程启动的事务。这违反了交易的ACID属性。

最简单的解决方案是拥有一个静态类级别sessionfactory,它将在应用程序的生命周期中仅返回一次sessionfactory。

    import org.hibernate.SessionFactory;
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    public class UserDao {
    private static SessionFactory factory;
    public static int register(Register u){
        int i=0;

        factory = getSessionFactory();
        Session session = factory.openSession();

        Transaction t = session.beginTransaction();
        t.begin();

        i=(Integer)session.save(u);
        t.commit();
        session.close();
        return i;
    }



public static SessionFactory getSessionFactory(){
        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml");

        if(factory==null)
         factory = cfg.buildSessionFactory();

        return factory;
    }
  }

虽然这是非常基本的解决方案,但我建议使用标准JEE注释或Spring的事务管理注释来处理事务。