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

时间:2015-01-21 07:42:59

标签: java hibernate

我的代码使用hibernate存在一些问题。 我的代码如下。

HibernateUtil.java


import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

/**
 * get sessionFactory
 * 
 * @author YD
 *
 */
public class HibernateUtil {
    private static SessionFactory sessionFactory;

    static {
        Configuration cfg = new Configuration().configure();
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(cfg.getProperties()).build();
        sessionFactory = cfg.buildSessionFactory(serviceRegistry);
    }

    /**
     * get sessionFactory from singleton pattern 
     * 
     * @return
     */
    public static SessionFactory getInstance() {
        if (sessionFactory == null) {
            Configuration cfg = new Configuration().configure();
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                    .applySettings(cfg.getProperties()).build();
            sessionFactory = cfg.configure().buildSessionFactory(
                    serviceRegistry);
        }
        return sessionFactory;
    }
}

MainCode获取新闻

/**
 * get my news
 */
public void getNews() {
    Session session = HibernateUtil.getInstance().getCurrentSession();
    Transaction tx = session.beginTransaction();

    // some operation

    tx.commit();

}

hibernate.cfg.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">
            mysql_url
        </property>
        <property name="hibernate.connection.username">my_username</property>
        <property name="connection.password">my_password</property>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>


        <!-- c3p0 connection pool -->
        <property name="org.hibernate.c3p0.internal.C3P0ConnectionProvider">org.hibernate.connection.C3P0ConnectionProvider</property>
        <property name="hibernate.c3p0.max_size">20</property>
        <property name="hibernate.c3p0.min_size">5</property>
        <property name="hibernate.c3p0.timeout">120</property>
        <property name="hibernate.c3p0.max_statements">100</property>
        <property name="hibernate.c3p0.idle_test_period">120</property>
        <property name="hibernate.c3p0.acquire_increment">2</property>
        <property name="hibernate.c3p0.validate">true</property>

        <!-- mapping file -->
        <mapping class="my.News />

    </session-factory>
</hibernate-configuration>

我也在我的项目中使用struts2。和我在tomcat 7.0.57上部署的web项目中的项目。

问题是:

当我打开网页获取新闻时,有时我得到例外:


Struts Problem Report

Struts has detected an unhandled exception:

Messages:   
nested transactions not supported
File:   org/hibernate/engine/transaction/spi/AbstractTransactionImpl.java
Line number:    154
Stacktraces

org.hibernate.TransactionException: nested transactions not supportedorg.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:154)
    org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1435)
    sun.reflect.GeneratedMethodAccessor59.invoke(Unknown Source)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:356)

为什么吗

我知道如果我使用 session.getCurrentSession()**来操作会话,那么就没有必要来关闭会话。我每次都提交交易。怎么回事?

0 个答案:

没有答案