有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么在缺少@Transactional时不抛出TransactionRequiredException?

我有一个Spring/JPA配置,其中Hibernate作为持久性提供者。但是,我不明白为什么在没有打开事务的情况下对以下DAO代码调用save()时没有抛出TransactionRequiredException(DAO/服务中没有@Transactional):

@Repository
public class UserDao {

    @PersistenceContext
    private EntityManager entityManager;

    public void save(User user) {
        entityManager.persist(user);
    }
}

正如预期的那样,实体未保存,但为什么没有引发异常?javadoc for persist表示persist()应该抛出“TransactionRequiredException-如果在PersistenceContextType.transaction类型的容器管理实体管理器上调用时没有事务”

调试显示Hibernate有一个打开的会话。这是预期的行为吗?顺便说一句:我在使用EclipseLink时也有同样的行为

这是我的应用程序上下文。xml:

<?xml version="1.0" encoding="UTF-8"?>
     <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  xmlns:context="http://www.springframework.org/schema/context"   xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<context:component-scan base-package="com.foo.bar" />
<context:annotation-config />
<tx:annotation-driven />

<!-- Configure jdbc.properties -->
<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/WEB-INF/jdbc.properties" />

<!-- Data Source configuration -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="HSQL" />
            <property name="showSql" value="true" />
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="persistenceExceptionTranslationPostProcessor"
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

编辑 Hibernate版本(来自pom.xml)

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.3.5.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.5.Final</version>
    <exclusions>
        <exclusion>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
        </exclusion>
        <exclusion>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.0-api</artifactId>
    <version>1.0.1.Final</version>
</dependency>

共 (2) 个答案

  1. # 1 楼答案

    javadoc声明,如果在PersistenceContextType.transaction类型的容器管理实体管理器上调用时没有事务,则应出现异常 您的上下文由Spring管理,我不认为Spring是JavaEE容器,因此同样的规则不适用,并且上下文可能默认为extended,以便在加入事务之前保存对象。如果要在事务不可用时强制执行异常,请在persist之后调用em.flush()

  2. # 2 楼答案

    答案非常清楚和简洁(实际上是你自己发现的):JPA需要一个正在进行的事务来调用EntityManager.persist(…)。但是,您的代码没有设置为在事务中运行。您的配置看起来很正常,但在DAO或上面的任何层上都缺少一个@Transactional

    由于最初的问题是为什么不抛出异常,我再次查看了规范,发现在7.9.1中,它显然要求容器在容器管理的持久性上下文中抛出该异常。在Spring中运行JPA是一种介于两者之间的场景,因为我们显然不需要JTA之类的东西,但是通过注入EntityManager可以理解,您假定处于一个托管环境中。我提交了SPR-11923以在我们的JPA基础设施支持中改进这一点

    旁道:在使用Spring和JPA时,您可能想看看Spring Data JPA project,它大大简化了构建数据访问层的过程,包括存储库层上事务处理的正常默认设置(例如,请参见此guide)。使用它本来可以防止你遇到这种情况