本文介绍了休眠:非法尝试将代理与两个打开的会话相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2种方法:
public static Ticket issueTicket(User user,Service service,String[] seats) {
Session ticSess= DB.factory.openSession();
ticSess.beginTransaction();
Date d= new Date();
Ticket ticket=new Ticket(d, service, user);
ticSess.save(ticket);
ticSess.getTransaction().commit();
int seatCount=seats.length;
for (int i=0;i<seatCount;i++){
int seatID=Integer.parseInt(seats[i]);
Seat seat=getSeatByID(seatID);
seat.setTicket(ticket);
ticSess.update(seat);
}
return ticket;
}
和
public static Seat getSeatByID(int seatID) {
Session proSess = DB.factory.openSession();
proSess.beginTransaction();
Seat c = (Seat) (proSess.load(Seat.class, seatID));
proSess.getTransaction().commit();
return c;
}
当我调用issueTicket方法时,我得到:
when I call issueTicket method I get:
illegally attempted to associate a proxy with two open Sessions
,如果我使用getSeatByID方法关闭会话,则会出现另一个错误,表明该会话已关闭.这是堆栈跟踪:
and If I close the session in getSeatByID method there will be another error telling that the session is closed. Here is the Stack Trace:
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:164)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:285)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
at ir.ac.shirazu.cse.Terminal.Seat_$$_javassist_9.setTicket(Seat_$$_javassist_9.java)
at ir.ac.shirazu.cse.Database.DB.issueTicket(DB.java:231)
推荐答案
尝试在返回前关闭getSeatByID()
中的proSess
.当前Seat
确实仍然与getSeatByID()
中打开的会话保持联系.
Try closing proSess
in getSeatByID()
before returning. Currently the Seat
indeed remains attached to session opened in getSeatByID()
.
这篇关于休眠:非法尝试将代理与两个打开的会话相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!