0

我有一个简单的独立应用程序来测试 Spring 的事务管理。拥有 Oracle Express Edition。运行以下命令以启用 XA

grant select on sys.dba_pending_transactions to user_test;
grant select on sys.pending_trans$ to user_test;
grant select on sys.dba_2pc_pending to user_test;
grant execute on sys.dbms_system to user_test;

我的 Java 代码大致如下:

public class DbUpdater 
{
    private static final ApplicationContext context = 
       new ClassPathXmlApplicationContext(new String[] {"spring_transactions.xml"});
    private static Logger log = LoggerFactory.getLogger(DbUpdater.class);

    @Transactional(propagation=Propagation.REQUIRED, readOnly=false)
    public void updateData()  {
        IMasterDAO ds1 = context.getBean("masterDao", IMasterDAO.class);
        log.info("Insert using ds1");
        ds1.insert("insert into users values(?,?)", "user1", "John Hamilton");
        log.info("Insert using ds1 finished successfully");

        throw new RuntimeException("A runtime exception");
    }
}

所以所有的想法都是看到事务回滚。我运行了几个配置示例,并且记录一直被提交。不执行回滚。没有错误没有,只有预期

Exception in thread "main" java.lang.RuntimeException: A runtime exception
at com.test.spring.transation.DbUpdater.updateData(DbUpdater.java:22)

我的最后一个配置是这样的:

    <bean id="txManager"
        class="org.springframework.transaction.jta.JtaTransactionManager">
            <property name="transactionManager" ref="bitronixTransactionManager" />
            <property name="userTransaction" ref="bitronixTransactionManager" />
    </bean>

    <bean id="btmConfig" factory-method="getConfiguration"
         class="bitronix.tm.TransactionManagerServices">
         <property name="serverId" value="spring-btm" />
    </bean>

    <bean id="bitronixTransactionManager" factory-method="getTransactionManager"
        class="bitronix.tm.TransactionManagerServices" 
                depends-on="btmConfig,dataSource"
        destroy-method="shutdown" />

    <bean id="dataSource" class="bitronix.tm.resource.jdbc.PoolingDataSource" 
      init-method="init" destroy-method="close">
    <property name="className" value="oracle.jdbc.xa.client.OracleXADataSource"/>
    <property name="uniqueName" value="myOracleDataSource"/>
    <property name="minPoolSize" value="0"/>
    <property name="maxPoolSize" value="5"/>
    <property name="allowLocalTransactions" value="true"/>
    <property name="testQuery" value="select sysdate from dual"/>
    <property name="driverProperties">
        <props>
            <prop key="user">${jdbc.username}</prop>
            <prop key="password">${jdbc.password}</prop>
            <prop key="URL">${jdbc.url}</prop>
        </props>
    </property>
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
</bean>

<bean id="masterDao" class="com.test.spring.transation.MasterDAO">
    <property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>

</beans>
4

1 回答 1

1

通过提供的数据源实例化的 JDBC 模板似乎与它自己的事务一起工作 [通过自动开始和提交事务]。在单独的提交/回滚周期中运行之后抛出异常,因此您会看到写入持续存在。要验证它,您可以移动代码以在 MasterDAO 类中引发异常并检查回滚。

于 2013-04-16T09:26:57.357 回答