关于java:Transaction在ApplicationContext的子方法中未激活

Transaction not active in child method of ApplicationContext

我只能在Oracle 11.2上运行的spring (3.0.5) jdbc application中获得活动的编程交易,
txAdvice指向从应用程序上下文(example"AAA" below)调用的实际方法(mainTest())时。
如果mainTest() (example"BBB" below)的子方法(transactionTest())txAdvice points,则我不再具有活动事务。

相关代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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文件是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?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>



<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()"方法,而忽略了方面。

我想,如果您想调用" transactionTest()"方法的动态创建的代理,则可以通过自我bean引用(注入)来实现。据我所记得,spring手册曾经建议通过接口引用(再次使用注入的接口引用)调用此类方法。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<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手册中有充分的记录(我在Aspects部分中回顾过),因此您一定应该阅读它:http://docs.spring.io/spring-framework/docs/current/spring-framework -reference / html / aop.html第9.6.1节

此外,这里已经解决了这个问题:一个Service方法调用Spring事务的内部多重方法