gpt4 book ai didi

java - ApplicationContext 的子方法中事务未激活

转载 作者:行者123 更新时间:2023-11-30 03:45:40 28 4
gpt4 key购买 nike

我只能在 Spring (3.0.5) jdbc 应用程序(在 Oracle 11.2 上运行)中获取 Activity 的编程事务,当txAdvice指向从应用程序上下文调用的实际方法(mainTest())(下面的示例“AAA”)。如果txAdvice指向mainTest()的子方法(transactionTest())(下面的例子“BBB”)那么我不再有活跃的交易。

相关代码:

public class MainDS {
public static Controller cont;
public static void main(String[] args) {

String [] configList ={"spring.xml"};

ApplicationContext ctx = new ClassPathXmlApplicationContext(configList);
cont = (Controller)ctx.getBean("controller");
cont.mainTest();
}
}

public class Controller {
private JdbcTemplate jdbcTemplate;

public void mainTest()
{
transactionTest();
// MainDS.cont.transactionTest(); // also does not work
}

public void transactionTest(){
try {
// This prints "Transaction active = true" for AAA but "Transaction active = false" for BBB
System.out.println("Transaction active = " + TransactionSynchronizationManager.isActualTransactionActive() );

} catch (SQLException e) {
e.printStackTrace();
}
}

public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}

我的 spring.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">


<bean id="controller" class="Controller">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>


<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="main*" propagation="REQUIRED" /> <!-- AAA -->
<tx:method name="transaction*" propagation="REQUIRED" /> <!-- or BBB -->
</tx:attributes>
</tx:advice>



<aop:config>
<aop:pointcut id="myMethods" expression="execution(* *..Controller.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="myMethods" />
</aop:config>



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

<bean id="dataConfigPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="searchSystemEnvironment" value="true" />
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="initialSize" value="2" />
<property name="maxActive" value="2" />

<property name="url" value="my connection details" />
<property name="username" value="xxx" />
<property name="password" value="xxx" />
</bean>

</beans>

我如何获得期权 BBB 的活跃交易。非常欢迎任何想法。

问候DS

最佳答案

“BBB”情况:“mainTest()”方法直接调用“transactionTest()”方法,省略了aspect。

如果你想调用“transactionTest()”方法动态创建的代理,我想,你可以通过 self bean 引用(注入(inject))来完成。据我所知,spring 手册曾经建议通过接口(interface)引用调用此类方法(再次使用注入(inject)的接口(interface)引用)。

注意:这是一个非常糟糕的设计!实现示例(根据请求):

<bean id="controller" class="test.Controller">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
<property name="instance" ref="controller" />
</bean>

public class Controller {
private JdbcTemplate jdbcTemplate;
private Controller instance;

public void mainTest() {
instance.transactionTest();
}

public void transactionTest() {
System.out.println("Transaction active = " + TransactionSynchronizationManager.isActualTransactionActive() );
}

public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

public Controller getInstance() {
return instance;
}

public void setInstance(Controller instance) {
this.instance = instance;
}
}

Transaction active = true

这种行为在 spring 手册中有详细记录(我记得它在方面部分),所以你一定应该阅读它:http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/aop.html第9.6.1节

此外,这个问题已经在这里讨论过:One Service method invoke inner multiple method for Spring transaction

关于java - ApplicationContext 的子方法中事务未激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25784873/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com