欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  资讯频道

Ebean ORM v1.0.0发布,增加了对@Transactional的支持

程序员文章站 2022-07-13 08:53:35
...

  Ebean可以提高您POJOs对新增事务的支持,可以在方法和类上使用@Transaction注释。之后Ebean将添加事务的逻辑(事务的开始,提交,回滚,暂停以及恢复事务等)。

使用@Transactional的例子:

// any old pojo  
 public class MyService {  
   
     @Transactional  
     public void runFirst() throws IOException {  
   
         // run in a Transaction (REQUIRED is the default)  
   
         // find a customer  
         Customer customer = Ebean.find(Customer.class, 1);  
   
         // call another "transactional" method  
         runInTrans();  
     }  
   
     @Transactional(type=TxType.REQUIRES_NEW)  
     public void runInTrans() throws IOException {  
   
         // run in its own NEW transaction   
         // ... suspend an existing transaction if required  
         // ... and resume it when this method ends  
   
         // find new orders  
         List<Order> orders = Ebean.find(Order.class)  
                                     .where().eq("status",OrderStatus.NEW)  
                                     .findList();  
         ...  


TxRunnable和TxCallable相当于@Transactional的方案,如果你喜欢还可以将 TxRunnable和TxCallable与@Transactional组合使用。

使用TxRunnable和TxCallable的例子:
 
 public void myMethod() {  
      ...  
      System.out.println(" Some code in myMethod...");  
      
      // run in Transactional scope...   
      Ebean.execute(new TxRunnable() {  
        public void run() {  
             
            // code running in "REQUIRED" transactional scope  
           // ... as "REQUIRED" is the default TxType  
           System.out.println(Ebean.currentTransaction());  
             
           // find stuff...  
           User user = Ebean.find(User.class, 1);  
           ...  
             
           // save and delete stuff...  
           Ebean.save(user);  
           Ebean.delete(order);  
           ...  
       }  
    });  
    
    System.out.println(" more code in myMethod...");  
  }  


你可以查看更多 @Transactional TxRunnable & TxCallable 的详情。
相关标签: ORM