JpaRepository-数据库操作在没有@Transactional的情况下工作

时间:2016-09-28 16:54:25

标签: hibernate jpa spring-data-jpa

我正在使用JPA存储库。以下是配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <tx:annotation-driven transaction-manager="transactionManager"/> 
    <jpa:repositories base-package="com.ca.dao"></jpa:repositories>

    <beans:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <beans:property name="location">
            <beans:value>classpath:database.properties</beans:value>
        </beans:property>
    </beans:bean>
    <beans:bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <beans:property name="dataSource" ref="dataSource"></beans:property>
        <beans:property name="packagesToScan" value="com.ca.bean"></beans:property>
        <beans:property name="jpaVendorAdapter">
            <beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></beans:bean>
        </beans:property>
        <beans:property name="jpaProperties">
            <beans:props>
                <beans:prop key="hibernate.hbm2ddl.auto">validate</beans:prop>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>
    <beans:bean id="dataSource" class="com.ca.common.CustomDataSource">
        <beans:property name="driverClassName" value="${jdbc.driverClassName}"></beans:property>
        <beans:property name="url" value="${jdbc.url}"></beans:property>
        <beans:property name="username" value="${jdbc.username}"></beans:property>
        <beans:property name="password" value="${jdbc.password}"></beans:property>
    </beans:bean>
    <beans:bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <beans:property name="entityManagerFactory" ref="entityManagerFactory"></beans:property>
    </beans:bean>
</beans:beans>

POM条目:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.1.1.Final</version>
    </dependency>
<dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.10.4.RELEASE</version>
    </dependency>

以下是JPA课程:

package com.ca.dao.client;

import java.io.Serializable;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

import com.ca.bean.client.ClientBean;

@Repository
public interface ClientDAO  extends JpaRepository<ClientBean, Serializable>, JpaSpecificationExecutor<ClientBean>{

}

以下是我尝试将bean保存到DB中的服务类

package com.ca.service.impl;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ca.dao.client.ClientDAO;

@Service
public class ScheduleTaskServiceImpl implements ScheduleTaskService {

    @Autowired
    private ClientDAO clientDAO;

    @Override
    @Transactional
    public void create(ClientBean cb) {
        clientDAO.save(db);
        throw new RuntimeException("sdfdsf");
    }
}

在上面的服务类中,我在保存详细信息后抛出运行时异常。问题是在抛出运行时异常后,事务没有得到回滚。在DB中仍然会创建条目 即使在删除@Transactional之后,我也看到了相同的行为。这意味着即使我将删除@Transactional,它也会保存数据库中的条目。

这里有什么问题?请帮忙。

即使我删除@Transactional

,数据也会插入到数据库中

0 个答案:

没有答案