[소스상에서 Transaction 처리]
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
public class DemoServiceImpl extends JdbcDaoSupport implements DemoService {
 
    private TransactionTemplate txTemplate;
     
    public void setTransactionManager(PlatformTransactionManager txManager) {
        this.txTemplate = new TransactionTemplate(txManager);
        this.txTemplate.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRED);
    }
     
    public int selectTest() throws Exception{
        // Transaction Handling
        txTemplate.execute(new TransactionCallbackWithoutResult() {
            public void doInTransactionWithoutResult(TransactionStatus status) {
                int cnt = getJdbcTemplate().update("insert into ddakker_test(SEQ, TXT)values(?,?)", new Object[]{"4","txt4"});
                System.out.println("insert cnt: " + cnt);
                int cnt2 = getJdbcTemplate().update("insert into ddakker_test(SEQ, TXT1)values(?,?)", new Object[]{"5","txt5"});
                System.out.println("insert cnt2: " + cnt2);            
            }          
        });
         
        List  list;
        Object params[] = {3};     
        list = getJdbcTemplate().queryForList("select seq, txt from ddakker_test where seq = ?", params);
         
        System.out.println("list1: " + list.size());
        Iterator i = list.iterator();
        System.out.println("i : " + i);
        while(i.hasNext()){
            Map map = (Map) i.next();
            System.out.println("map" + map);
        }
         
        return 0;
    }
}
1
2
3
4
5
6
7
8
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource">
</property></bean>
 
<bean id="demoService" class="demo.DemoServiceImpl">
    <property name="dataSource" ref="dataSource">
    <property name="transactionManager" ref="transactionManager">
</property></property></bean>

[설정파일에서 Transaction 처리]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Demo2Servicelmpl extends JdbcDaoSupport implements Demo2Service {
     
    public int selectTest() throws Exception {     
        int cnt = getJdbcTemplate().update("insert into ddakker_test(SEQ, TXT)values(?,?)", new Object[]{"4","txt4"});
        System.out.println("insert cnt: " + cnt);
        int cnt2 = getJdbcTemplate().update("insert into ddakker_test(SEQ, TXT1)values(?,?)", new Object[]{"5","txt5"});
        System.out.println("insert cnt2: " + cnt2);
         
        List  list;
        Object params[] = {3};     
        list = getJdbcTemplate().queryForList("select seq, txt from ddakker_test where seq = ?", params);
         
        System.out.println("list1: " + list.size());
        Iterator i = list.iterator();
        System.out.println("i : " + i);
        while(i.hasNext()){
            Map map = (Map) i.next();
            System.out.println("map" + map);
        }
         
        return 0;
    }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<bean id="demo2ServiceTarget" class="demo.Demo2Servicelmpl">
    <property name="dataSource" ref="dataSource">
</property></bean>
 
<bean id="demo2Service" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="transactionManager">
    <property name="target" ref="demo2ServiceTarget">
    <property name="proxyTargetClass" value="true">
    <property name="transactionAttributes">
        <props>
            <prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
            <prop key="get*">PROPAGATION_SUPPORTS</prop>
        </props>
    </property>
</property></property></property></bean>

[설정파일에서 Transaction 처리(AspectJ 를 이용한 한방 처리)]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource">
    </property></bean>
     
    <bean id="demo2Service" class="demo.Demo2Servicelmpl">
        <property name="dataSource" ref="dataSource">
    </property></bean
     
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" timeout="1000">
        </tx:method></tx:attributes>
    </tx:advice>
         
    <aop:config>
        <aop:advisor pointcut="execution(* demo.Demo2Servicelmpl.*(..))" order="1" advice-ref="txAdvice">
    </aop:advisor></aop:config>
</beans>

+ Recent posts