Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java hibernate 4.3.7最终org.hibernate.TransactionException:不支持嵌套事务_Java_Hibernate - Fatal编程技术网

Java hibernate 4.3.7最终org.hibernate.TransactionException:不支持嵌套事务

Java hibernate 4.3.7最终org.hibernate.TransactionException:不支持嵌套事务,java,hibernate,Java,Hibernate,我使用hibernate的代码有一些问题。 我的代码如下 HibernateUtil.java main代码以获取实际新闻 hibernate.cfg.xml 为什么? 我知道如果我使用session.getCurrentSession()**来操作会话,那么**就没有必要关闭会话。我每次最后都提交事务。它是如何发生的?如果//some操作失败会发生什么?是否回滚事务?如果某些操作失败,是否需要手动回滚事务? import org.hibernate.SessionFactory; impor

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

HibernateUtil.java


main代码以获取实际新闻 hibernate.cfg.xml 为什么?
我知道如果我使用session.getCurrentSession()**来操作会话,那么**就没有必要关闭会话。我每次最后都提交事务。它是如何发生的?

如果//some操作失败会发生什么?是否回滚事务?如果
某些操作失败,是否需要手动回滚事务?
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;
    }
}
/**
 * get my news
 */
public void getNews() {
    Session session = HibernateUtil.getInstance().getCurrentSession();
    Transaction tx = session.beginTransaction();

    // some operation

    tx.commit();

}
<?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>
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)