Spring data-jpa Transactional不适用于服务

时间:2013-11-25 10:44:29

标签: spring jpa spring-data transactional

这是我的服务方法:

@Resource
private PersonRepository personRepository;

@Transactional
@Override
public Person create(PersonDTO created) {
    LOGGER.debug("Creating a new person with information: " + created);

    Person person = Person.getBuilder(created.getFirstName(), created.getLastName()).build();

    personRepository.save(person);

    foo();

    return null;
}
public void foo() {
    throw new RuntimeException();
}

正如你所看到的那样,我正在抛出一种执行,但坚持不懈地发生。

这是我的数据源配置:

<tx:annotation-driven transaction-manager="transactionManager"  proxy-target-class="true" />
 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/dbb"/>
    <property name="username" value="root" />
    <property name="password" value="pass" />
    <property name="initialSize" value="10"/>
    <property name="maxActive" value="100"/>
    <property name="maxIdle" value="15"/> 
    <property name="minIdle" value="10"/> 
    <property name="timeBetweenEvictionRunsMillis" value="10000"/> 
    <property name="minEvictableIdleTimeMillis" value="60000"/> 
    <property name="validationQuery" value="/* ping */ SELECT 1"/>
    <property name="testOnBorrow" value="true"/> 
    <property name="testWhileIdle" value="true"/>           
    <property name="removeAbandoned" value="true"/> 
    <property name="removeAbandonedTimeout" value="300"/> 
</bean>

  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="punit"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL"/>
            <!-- <property name="databasePlatform" value="${hibernate.dialect}"/> -->
            <property name="showSql" value="false"/>               
            <property name="generateDdl" value="false"/>
            <!--  <property name="hibernate.connection.autocommit" value="false"/> -->
        </bean>
    </property>
    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.connection.autocommit" value="false" />
        </map>
    </property>
</bean> 

知道为什么吗?

谢谢!

1 个答案:

答案 0 :(得分:-3)

你必须抛出TransactionSystemException Spring。否则,您必须抛出扩展TransactionSystemException

的异常

示例:

import org.springframework.transaction.TransactionSystemException;

public class SystemException extends TransactionSystemException {
    public SystemException(String message) {
        super(message);
    }

    public SystemException(String message, Throwable throwable) {
        super(message, throwable);
    }
}

public void foo() {
    throw new SystemException("System Error!");
}