Skip to content

Instantly share code, notes, and snippets.

@gnosis23
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save gnosis23/3c5f989dc22c25e27116 to your computer and use it in GitHub Desktop.

Select an option

Save gnosis23/3c5f989dc22c25e27116 to your computer and use it in GitHub Desktop.
transaction propagation
package service.impl;
import entity.Message;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import service.FooService;
/**
* Created by wang.bh
*/
public class DefaultFooService implements FooService {
private DriverManagerDataSource dataSource;
private InnerService innerService;
@Override
public Message getMessage(String fooName) {
throw new UnsupportedOperationException();
}
@Override
public Message getMessage(String fooName, String barName) {
throw new UnsupportedOperationException();
}
@Override
public void insertFoo(Message msg) {
throw new UnsupportedOperationException();
}
@Override
public void updateFoo(Message msg) {
JdbcTemplate template = new JdbcTemplate(dataSource);
template.update("update USER_INFO set age = age + 20 where UNAME = 'tangmaru'");
//throw new RuntimeException("in failed");
//System.out.println("in success!");
}
@Override
public void require() {
JdbcTemplate template = new JdbcTemplate(dataSource);
try {
// Trans B
innerService.updateFoo(new Message());
} catch (Exception e) {
System.out.println("update failed: " + e.getMessage());
}
template.update("update USER_INFO set age = age + 10");
//insertFoo(new Message());
System.out.println("out success!");
}
public void setDataSource(DriverManagerDataSource dataSource) {
this.dataSource = dataSource;
}
public void setInnerService(InnerService innerService) {
this.innerService = innerService;
}
}
package service;
import entity.Message;
/**
* Created by wang.bh
*/
public interface FooService {
Message getMessage(String fooName);
Message getMessage(String fooName, String barName);
void insertFoo(Message msg);
void updateFoo(Message msg);
void require();
}
package service.impl;
import entity.Message;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
/**
* @author: bohao
*/
public class InnerService {
private DriverManagerDataSource dataSource;
public void updateFoo(Message msg) {
JdbcTemplate template = new JdbcTemplate(dataSource);
template.update("update USER_INFO set age = age + 20 where UNAME = 'tangmaru'");
throw new RuntimeException("in failed");
//System.out.println("in success!");
}
public void setDataSource(DriverManagerDataSource dataSource) {
this.dataSource = dataSource;
}
}
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean class="service.ProgrammaticFooService" id="programmaticFooService">
<constructor-arg name="transactionManager" ref="txManager"/>
<constructor-arg name="dataSource" ref="dataSource" />
</bean>
<bean id="fooService" class="service.impl.DefaultFooService" >
<property name="dataSource" ref="dataSource" />
<property name="innerService" ref="inner" />
</bean>
<bean id="inner" class="service.impl.InnerService" >
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="insert*" />
<tx:method name="update*" propagation="REQUIRES_NEW" />
<tx:method name="require" propagation="REQUIRES_NEW" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="fooServiceOperation"
expression="execution(* service.impl.*Service.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation" />
</aop:config>
</beans>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment