Spring Transaction - 프로그램에서 처리

2010. 8. 22. 20:04plming/Java - Spring

- 프로그램에서 트랜잭션 처리

 import org.springframework.transaction.PlatformTransactionManager;
 import org.springframework.transaction.TransactionDefinition;
 import org.springframework.transaction.TransactionStatus;
 import org.springframework.transaction.support.DefaultTransactionDefinition;

 public class PragrammaticBoardService implements BoardService {

     private PlatformTransactionManager transactionManager;

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

     private TransactionDefinition getDefinition(int isolationLevel,
             boolean isReadOnly) {
         DefaultTransactionDefinition def = new DefaultTransactionDefinition(
                 TransactionDefinition.PROPAGATION_REQUIRED);
         def.setTimeout(5000);
         def.setReadOnly(isReadOnly);
         def.setIsolationLevel(isolationLevel);
         return def;
     }

     public Board addBoard(Board board) {
         TransactionStatus status = transactionManager
                 .getTransaction(getDefinition(
                         TransactionDefinition.ISOLATION_READ_COMMITTED, false));

         try {
             boardFileDAO.insert(result.getBoardNo(), board.getBoardFiles());
             transactionManager.commit(status);
         } catch (Throwable t) {
             transactionManager.rollback(status);

             if (t instanceof RuntimeException) {
                 throw (RuntimeException) t;
             }
         }
         return result;
     }

     public Board updateBoard(Board board) {
         TransactionStatus status = transactionManager
                 .getTransaction(getDefinition(
                         TransactionDefinition.ISOLATION_READ_COMMITTED, false));
         ::::

     public Board findBoard(int boardNo) {
         TransactionStatus status = transactionManager
                 .getTransaction(getDefinition(
                         TransactionDefinition.ISOLATION_READ_COMMITTED, true));
         ::::


 - applicationContext-jdbc.xml

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

 - applicationContext-board.xml

 <bean id="boardService" class="net.javajigi.board.service.PragrammaticBoardService">
     <property name="transactionManager">
         <ref bean="transactionManager" />
     </property>
 </bean>


[출처] Spring 프레임워크 워크북

'plming > Java - Spring' 카테고리의 다른 글

모델 1 개발 vs 모델 2 개발  (0) 2010.08.26
Spring Transaction - 선언적으로 처리  (0) 2010.08.22
Spring Transaction  (0) 2010.08.22
Spring - 빈 설정 파일 관리  (0) 2010.08.22
Spring AOP - Logging, Exception 구현  (0) 2010.08.19