Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring@Transaction未在RuntimeException上回滚_Spring_Jpa_Rollback_Jdbctemplate - Fatal编程技术网

Spring@Transaction未在RuntimeException上回滚

Spring@Transaction未在RuntimeException上回滚,spring,jpa,rollback,jdbctemplate,Spring,Jpa,Rollback,Jdbctemplate,我希望在代码中发生异常时支持回滚 我在Spring配置文件中使用junit+数据源进行测试,而Glassfish 2.1用于实际代码(使用jndi数据源) 这里是一个代码示例 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:test-web-spring-config.xml" }) public class PersistTest { @Autowired

我希望在代码中发生异常时支持回滚

我在Spring配置文件中使用junit+数据源进行测试,而Glassfish 2.1用于实际代码(使用jndi数据源)

这里是一个代码示例

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:test-web-spring-config.xml" })
public class PersistTest {

    @Autowired
    Transformer transformer;

    @Before
    public void setUp() throws Exception {

    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    @Transactional("transactionManagerTest")
    //@Rollback(false)
    // @Ignore
    public void test() {

        transformer.export();

    }

}

@Component
public class Transformer {

    @Autowired
    ContextPersist context;

    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void export(){
        //code here
        // persist here
        context.persist();
        // to test a rollback
        throw new RuntimeException("testing rollback2");

    }

}

@Component
public class ContextPersist {

    @Autowired
    @Qualifier(value = "dataSource")
    DataSource dataSource;

    // bulk insert
    JdbcTemplate jdbcTemplate;

    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void persist() {
        jdbcTemplate = new JdbcTemplate(dataSource);

        //.. here I insert data with jdbcTemplate.batchUpdate(....)

        // to test a rollback
        throw new RuntimeException("testing rollback1");
    }
}
该代码不会回滚

如果我在Junit中使用@Rollback(true),事务将回滚。但我需要在少年外面有同样的行为

编辑:(添加了spring配置)

我的项目包含一个webapp(demo.war)和一个用于DAO+businessrules的jar

在我的webapp中,我有我的变压器

我在这个webapp中有一个父Spring配置,还有一个与其他webapp共享的公共Spring配置

这是文件

战争

web-spring-config.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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
         http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

    <context:component-scan base-package="com.test" />
    <tx:annotation-driven />
    <task:annotation-driven/>

    <import resource="classpath:common-spring-config.xml" />

</beans>
<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
        http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    <context:component-scan base-package="com.test" />
    <tx:annotation-driven />
    <task:annotation-driven/>

    <!-- Hibernate -->
    <bean id="hibernateSessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="demo.datasource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="hibernateSessionFactory" />
    </bean>

    <!-- datasource -->
    <bean id="demo.datasource" class="org.springframework.jndi.JndiObjectFactoryBean"
        lazy-init="true">
        <property name="jndiName" value="jdbc/demo" />
    </bean>

</beans>

刀罐

common-spring-config.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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
         http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

    <context:component-scan base-package="com.test" />
    <tx:annotation-driven />
    <task:annotation-driven/>

    <import resource="classpath:common-spring-config.xml" />

</beans>
<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
        http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    <context:component-scan base-package="com.test" />
    <tx:annotation-driven />
    <task:annotation-driven/>

    <!-- Hibernate -->
    <bean id="hibernateSessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="demo.datasource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="hibernateSessionFactory" />
    </bean>

    <!-- datasource -->
    <bean id="demo.datasource" class="org.springframework.jndi.JndiObjectFactoryBean"
        lazy-init="true">
        <property name="jndiName" value="jdbc/demo" />
    </bean>

</beans>

类路径:hibernate.cfg.xml
org.hibernate.cfg.AnnotationConfiguration
假的
org.hibernate.dialen.oracle10galent

您的事务管理器

org.springframework.orm.hibernate3.HibernateTransactionManager
但是您使用的是
JdbcTemplate

AOP配置为在hibernate操作上自动开始/提交/回滚。 jdbcTemplate不会参与任何事务。 无事务=无回滚。
它类似于连接。setAutoCommit(true)

@Rollback
仅在测试环境中相关。它对Spring应用程序没有任何意义。同意。我刚读到这件事。但玻璃鱼仍然存在这个问题。我的代码没有回滚。我需要得到调用:transformer.export();回滚。杜松子和玻璃鱼(杜松子外)。我试图测试,如果发生异常,我的代码不会提交数据。这就是为什么我在代码中硬编码了一个抛出异常。您正在使用哪个数据库,请发布您的配置。请注意,您的
ContextPersist
类不应在每次需要时构造
JdbcTemplate
,它是一个需要创建的沉重对象(因此创建一次),其次是线程安全的。接下来,确保使用基于类的代理(添加cglib并指定
proxy target class=“true”
给你的
。我确认我的转换器是线程安全的。我知道不应该重新创建jdbctemplate,但这只是简化此演示中的代码的一个步骤。它将只运行一个。但是感谢你的评论。我的代码似乎缺少代理目标参数。我将尝试它,看看它是否解决了问题。我知道,我正在寻找它o更改以使其工作。我不知道需要在配置和代码中添加什么,以便在我的演示中将事务注入JDBCTemplate。如果您只需要一个事务管理器(仅限jdbc操作),则可以在@Transactional value.@Transactional(“dataSourceTransactionManager”)上指定事务管理器