EJB의 JPA(사무 롤백)

1789 단어 jpaejb
package cn.com.leadfar.jpa;

import java.util.Random;

import javax.naming.InitialContext;
import javax.transaction.UserTransaction;

import cn.com.leadfar.jpa.StudentManager;

import junit.framework.TestCase;

public class StudentManagerClient extends TestCase{

    public void testAddStudent01() throws Exception{
        InitialContext context = new InitialContext();
        StudentManager sm = (StudentManager)context.lookup("StudentManager/remote");
        
        for(int i=0; i<10; i++){
            sm.addStudent(" "+new Random().nextInt(99999));
            if(i > 5){
                throw new RuntimeException(" !");
                // .
            }
        }
    }
    
    public void testAddStudent02() throws Exception{
        InitialContext context = new InitialContext();
        StudentManager sm = (StudentManager)context.lookup("StudentManager/remote");
        //JTA UserTransaction .
        UserTransaction utx = (UserTransaction)context.lookup("UserTransaction");
        
        
        try{
            utx.begin();
            // . .
            for(int i=0; i<10; i++){
                sm.addStudent(" "+new Random().nextInt(99999));
                if(i > 5){
                    throw new RuntimeException(" !");
                }
            }
            
            utx.commit();
        }catch(Exception e){
            utx.rollback();
            e.printStackTrace();
        }
    }
    
}

좋은 웹페이지 즐겨찾기