使用@Transactional注释时,JdbcTemplate不起作用

时间:2015-02-23 12:30:42

标签: java spring spring-data-jpa spring-jdbc

在同一函数中使用spring数据和jdbctemplate时 spring数据工作正常,但jdbc更新不起作用。

这是服务类。 jdbc更新然后没有发生错误但数据库没有更新

@Service
public class SampleService {
    private static final Logger logger = LoggerFactory.getLogger(SampleService.class);

    @Inject
    private SampleRepository sampleRepository;

    @Inject
    private JpaTransactionManager transactionManager;

    private JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(transactionManager.getDataSource());
    }

    @Transactional(propagation=Propagation.REQUIRED)
    public void execute(){

        sampleRepository.deleteAll();
        sampleRepository.save(new Sample("sample"));

        logger.info("data count (after jpa insert) : {}", sampleRepository.count());//1

        int count = jdbcTemplate().update("insert into Sample (sample) values ( ? )", "sample");
        logger.info("jdbc insert count: {}", count);//1

        logger.info("data count (after jdbc insert) : {}", sampleRepository.count());//expected: 2 , actual:1

        count = jdbcTemplate().queryForObject("select count(id) from Sample", Integer.class);
        logger.info("data count (after jdbc insert) : {}", count); //jdbc count query result also return 1

    }

当不使用@Transactional注释时,工作正常。

    public void execute2(){

        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setName("testTx");
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

        TransactionStatus status = transactionManager.getTransaction(def);

        ... this method work fine ...

        transactionManager.commit(status);

    }

}

这是数据访问配置。

<context:annotation-config />
<tx:jta-transaction-manager />
<tx:annotation-driven transaction-manager="transactionManager" />
<jpa:repositories base-package="example" />

<context:property-placeholder location="classpath:config.properties" />


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

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="DefaultUnit" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="${db.showsql}" />
            <property name="generateDdl" value="${db.generateDDL}" />
            <property name="database" value="${db.database}" /><!-- H2 -->
        </bean>
    </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
    <constructor-arg ref="basicDataSource" />
</bean>

<bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${db.driver}" />
    <property name="url" value="${db.url}" />
    <property name="username" value="${db.user}" />
    <property name="password" value="${db.pass}" />
    <property name="defaultAutoCommit" value="false" />
</bean>

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

0 个答案:

没有答案