熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Hibernate 基於JDBC的事務[4]

2022-06-13   來源: Java開源技術 

  Java代碼

public class ClassA{   
public void save(TUser user){   
session = sessionFactoryopenSession();   
sessionsave(user);   
sessionclose();   
}   
……   
}   
public class ClassB{   
public void save (Order order){   
session = sessionFactoryopenSession();   
sessionsave(order);   
sessionclose();   
}   
……   
}   
public class ClassC{   
public void save(){   
……   
UserTransaction tx = new InitialContext()lookup(……);   
classAsave(user);   
classBsave(order);   
txcommit();   
……   
}   

public class ClassA{
public void save(TUser user){
session = sessionFactoryopenSession();
sessionsave(user);
sessionclose();
}
……
}
public class ClassB{
public void save (Order order){
session = sessionFactoryopenSession();
sessionsave(order);
sessionclose();
}
……
}
public class ClassC{
public void save(){
……
UserTransaction tx = new InitialContext()lookup(……);
classAsave(user);
classBsave(order);
txcommit();
……
}
}
上面代碼中的ClassCsave方法也可以改成這樣
Java代碼
public class ClassC{   
public void save(){   
……   
session = sessionFactoryopenSession();   
Transaction tx = sessionbeginTransaction();   
classAsave(user);   
classBsave(order);   
txcommit();   
……   
}   

public class ClassC{
public void save(){
……
session = sessionFactoryopenSession();
Transaction tx = sessionbeginTransaction();
classAsave(user);
classBsave(order);
txcommit();
……
}
}

[]  []  []  []  []  


From:http://tw.wingwit.com/Article/program/Java/ky/201311/29040.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.