繁体   English   中英

不支持休眠、嵌套事务

[英]Hibernate, nested transactions not supported

我在运行我的 Web 应用程序时遇到了麻烦,该应用程序试图从表滑雪板中获取所有信息并将其放入一个列表中,然后我将在 xHtml 中打印出来。 但我在下面得到了这个例外。

org.hibernate.TransactionException: 不支持嵌套事务

问题是我不知道为什么会发生这种异常,所以希望得到一些解释。 此外,如果您在代码中发现任何问题,那将是非常棒的。

这是我得到的例外。

休眠实用程序

public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {



        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

SnowHelper,HelperClass

public class SnowHelper {

    Session session = null;

    public SnowHelper() {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    public List getSnowboards() {
        List<Snowboard> snowboardList = null;

        try {
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Snowboard");
            snowboardList = (List<Snowboard>) q.list();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return snowboardList;
    }
}

休眠配置

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://cpsrv01.misshosting.com:3306/etvffqgz_snowshop</property>
    <property name="hibernate.connection.username">etvffqgz_user</property>
    <property name="hibernate.connection.password">759486456</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>
    <mapping resource="Hibernate/Account.hbm.xml"/>
    <mapping resource="Hibernate/Snowboard.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

SnowboardBean、ManagedBean 类

@Named(value = "snowboardBean")
@Dependent
public class SnowboardBean {

    private List<Snowboard> snowList;
    private SnowHelper snow;
    /**
     * Creates a new instance of SnowboardBean
     */
    public SnowboardBean() {
        snowList = new ArrayList<>();
        snow = new SnowHelper();

        snowList = snow.getSnowboards();
    }

    /**
     * @return the snowList
     */
    public List<Snowboard> getSnowList() {
        return snowList;
    }

}

在这里.. 提交您的交易

Transaction tx = session.beginTransaction();
Query q = session.createQuery("from Snowboard");
snowboardList = (List<Snowboard>) q.list();
tx.commit();

否则,您只需在每次调用此方法时打开一个新事务而不关闭它。最终其中一个被打开,而另一个尚未提交。

如果您使用的是“容器管理事务”(由 EJB 的 Spring 提供),您就不必担心显式提交您的事务。 在这里,您使用的是“扩展事务管理”,您必须自己处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM