未设置任何事务自动提交

未设置任何事务自动提交

本文介绍了Spring JPA-未设置任何事务自动提交"true"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用JPA自动提交而不进行交易?

Is there any way to autocommit using JPA without transaction?

persistence.xml

persistence.xml

<persistence-unit name="mytest" transaction-type="RESOURCE_LOCAL">
    <properties>
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
    </properties>
</persistence-unit>

data-config.xml

data-config.xml

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="mytest" />
        <property name="dataSource" ref="myDataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="database" value="ORACLE" />
            </bean>
        </property>
        <property name="jpaPropertyMap">
            <map>
<entry key="hibernate.format_sql" value="false" />
                <entry key="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
                <entry key="hibernate.jdbc.fetch_size" value="0" />
                <entry key="hibernate.jdbc.batch_size" value="0" />
                <entry key="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
            </map>
        </property>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <qualifier value="mytest"/>
    </bean>

CODE

for(User user: users) {
    entityManager.merge(user);
    entityManager.flush();
}

但是在会话终止之前,我看不到提交.我想逐条记录提交.

But I am not able to see the commit until the session is terminated. I want to commit record by record.

推荐答案

不,JPA中没有自动提交,这也没有多大意义,因为对实体的每次更改都是最终会转到数据库的更改.使用自动提交,对属性的每个更改都将是一个事务.这是一个非常特殊的要求,不符合JPA的功能.

No, there is no autocommit in JPA, it doesn't make much sense either, since every change to an entity is a change that eventually goes to the database. With an autocommit, every single change to a property would be a transaction. That is a very special requirement, that doesn't qualify as a feature candidate for JPA.

但是您所需要做的就是将交易包装在 entityManager.merge(user);

But all you need to do is to wrap a transaction around entityManager.merge(user);

这篇关于Spring JPA-未设置任何事务自动提交"true"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 03:36