调试Hibernate“会话已关闭!” - 滥用SSB?
(首先,这不是我的代码。其次,为了保护有罪,我改变了班级名称以隐藏我的雇主特有的东西,所以如果事情彼此不相符,那就是为什么!)。
有人可以帮我解决疑问吗?我被要求调查完整连接池的问题,这个代码似乎闻到了。
我们的日志中有很多
org.hibernate.SessionException: Session is closed!
,围绕以下代码,它使用表现不佳的连接池。
电话通过网络服务进入:
1 @Stateless
2 @WebService(name="MyWebService", targetNamespace="http://www.mycompany.com/2010/01/WebService/myWebService")
3 @WebContext(contextRoot="/myContextRoot", secureWSDLAccess=false)
4 public class MyWebService implements IMyWebService {
6 @WebMethod
7 @NotNull
8 public ArrayList<SearchResult> performSearch(ArrayList<String> criteria) {
9 GetAllResponses caller = new GetAllResponses();
10 return caller.doSearch(criteria);
11 }
13 }
GetAllResponses
课程如下:
1 public class GetAllResponses {
2 private static MyHome myHome = new MyHome();
4 public SearchResult doSearch(ArrayList<String> criteria) {
5 return doSearchInternal(criteria.elementAt(0), criteria.elementAt(1));
6 }
8 private SearchResult doSearchInternal(String a, String b) {
9 DataObject info = myHome.findDataObject(a, b);
10 return info.getAsSearchResult();
11 }
12 }
并且MyHome
如下:
1 @Stateless
2 @Local({MyHomeInterface.class})
3 public class MyHome {
4 private Session session;
6 public DataObject findDataObject(String a, String b) {
7 try {
8 this.session = MyHibernateUtil.getSessionFactory().getCurrentSession();
9 Transaction tx = this.session.beginTransaction();
10 //do stuff with session and return a DataObject
11 } catch (Exception ex) {
12 ex.printStackTrace();
13 } finally {
14 this.session.close();
15 }
16 }
18 public DataObject doAnotherFind(String a, String b) {
19 try {
20 this.session = MyHibernateUtil.getSessionFactory().getCurrentSession();
21 Transaction tx = this.session.beginTransaction();
22 //do stuff with session and return a DataObject
23 } catch (Exception ex) {
24 ex.printStackTrace();
25 } finally {
26 this.session.close();
27 }
28 }
30 }
注意如何在GetAllResponses
的第2行将MyHome
类实例化为静态字段,并且在MyHome
的第8行和第20行上分配字段session
。
根据我对事物的理解,SSBMyHome
不是由J2EE服务器(JBoss 4.2.2 GA)管理的,因为它已被实例化为GetAllResponses
类的静态字段而不是在JNDI上查找。因此,两个线程可以同时访问同一个MyHome
的实例,并且由于会话存储在字段session
中,第一次调用很容易将其Session
替换为另一个Session
,导致各种问题,包括线路上的org.hibernate.SessionException: Session is closed!
MyHome
的9和21(作为一个例子,两个线程调用findDataObject
,第一个线程在第二个线程运行第8行之后且在它运行第9行之前运行第14行)。
我对么?
没有找到相关结果
已邀请:
1 个回复
厦惫