When the method in the DaO automatically generated by hibernate synchronizer 3.1.9 is used, a "session is closed." exception is reported.
For example, when the findbyname () method is used,
View the generatedCode, Found
Public java. util. List <mapping. User> findbyname (Java. Lang. string name ){
Return findfiltered ("name", name). List ();
}
The following method in _ baserootdao is finally called:
Protected criteria findfiltered (string propname, object filter, order ){
Session S = NULL;
Try {
S = getsession ();
Return findfiltered (S, propname, filter, order );
}
Finally {
Closesession (s );
}
}
(Note that the session is closed in finally)
When the list () method of criteria generated by this method is called, some code in the list () method checks that the session is closed and throws the above one.
Public list () throws hibernateexception {
Before ();
Try {
Return session. List (this );
}
Finally {
After ();
}
}
The List () method in the session:
Public list (criteriaimpl criteria) throws hibernateexception {
Errorifclosed ();
Checktransactionsynchstatus ();
String [] implementors = factory. getimplementors (criteria. getentityorclassname ());
Int size = implementors. length;
......
Errorifclosed ():
Protected void errorifclosed (){
If (closed ){
Throw new sessionexception ("session is closed! ");
}
}
The temporary solution is to remove the code for closing the session in finally above. ThenProgramRunning properly.
I am a beginner in hibernate. Please call us more. Thank you!