728x90
반응형

- 트랜잭션 매니저 bean정의하기

 

TransactionManger 정의

 

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


public class TransactionService1 {

    /** 트랜잭션 매니져 */
    private PlatformTransactionManager transactionManager;

    public void setTransactionManager(PlatformTransactionManager transactionManager) {
         this.transactionManager = transactionManager;
    }
      ...
}///~


- 프로그래밍적으로 트랜잭션 처리하기(1)

 

프로그래밍적으로 트랜잭션 처리 샘플

 

public void addPerson(Person person) {
    DefaultTransactionDefinition transDef =
         new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED);
    transDef.setReadOnly(false);
    transDef.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
    TransactionStatus status = transactionManager.getTransaction(transDef);
    Assert.notEmpty(person.getFamilies());
    String newId = this.createNewPersonId();
    person.setId(newId);
    ...
    person.setUpdateDate(createDateTime);
    try {
          .....
    transactionManager.commit(status);
    } catch (Exception e) {
        transactionManager.rollback(status);
        throw new RuntimeException(e);
    }
}//:


- 프로그래밍적으로 트랜잭션 처리하기(2-1)

 

프로그래밍적으로 트랜잭션 처리 샘플

 

<bean id="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager">
 <ref bean="transactionManager" />
</property>
</bean>


public class TransactionService4 {

    private PersonDao personDao;
    private FamilyDao familyDao;

    private TransactionTemplate transactionTemplate;

}///~

 

- 프로그래밍적으로 트랜잭션 처리하기(2-2)

 

프로그래밍적으로 트랜잭션 처리 샘플


public void addPerson(final Person person) {
final String newId = this.createNewPersonId();
person.setId(newId);
String createDateTime = DatetimeUtil.getCurrentDateTime().substring(0, 8);
person.setCreateDate(createDateTime);
person.setUpdateDate(createDateTime);
Object retObject = transactionTemplate.execute(
new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
      int rv = personDao.insertWithObject(person);
for(int i=0; i < person.getFamilies().length; i++) {
Family fbean = person.getFamilies()[i];
fbean.setId(person.getId());
rv = addFamily(fbean);
}// for
return null;
}//:
});
}//:

 

 

728x90
반응형
블로그 이미지

nineDeveloper

안녕하세요 현직 개발자 입니다 ~ 빠르게 변화하는 세상에 뒤쳐지지 않도록 우리모두 열심히 공부합시다 ~! 개발공부는 넘나 재미있는 것~!

,