Hello,
I am here to ask you a question about transaction and session management. Because I know you are experimented.
This is my problem :
When I want to save or get information from the data base, I use a session and do some operation on it like :
Code:
List<Object> results = session.createQuery("from File").list();
or
session.saveOrUpdate(File);
But when I want to save some data in data base, I need to begin a transaction and commit after the operation on session like this :
Code:
Transaction transaction = session.beginTransaction()
session.saveOrUpdate(File);
....
transaction.commit();
The problem is that I have implemented a DAOFile class in which I just made the operation on session.
And I hava a service that use this DAOFile class method, that I need to surrond with transaction...
A person tell me that I must use Interceptor in order to be sure to initialise this mecanism or transaction just one time during all the necessary operation. But I do not understand what to do know, because I do not understand why there are a lot of different session during one transaction and why I do not close this session and why, if I close one, the other operation does not work after....
This is my current architecture and maybe where comes the problem ?:
This is my HibernateUtil initialisation of configuration and session.
Code:
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
Configuration annotationConfiguration = new AnnotationConfiguration()
.addAnnotatedClass(Dossier.class)
annotationConfiguration.setInterceptor(new BDInterceptor());
sessionFactory = annotationConfiguration.configure().buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession() throws HibernateException {
return sessionFactory.openSession();
}
This is my DAO class :
Code:
public class DAODossierImpl {
...
public List<Object> getAll() {
Session session = HibernateUtil.getSession();
List<Object> results = session.createQuery("from Dossier").list();
return results;
}
This is my Service class that calls this DOA method :
Code:
class ServiceDossier {
public List<Dossier> getAll() {
List<Object> dossiers = getIDAO().getAll();
...
return dossiers;
}
And this is my Facade class that calls this service :
Code:
public class FacadeDossier {
public List<Dossier> getAllDossier() {
List<Dossier> dossiers = serviceDossier.getAll();
return dossiers;
}
I think to surrond Facade method with the Transaction init and end. But I do not understand why it was said to me to use an interceptor for this ?
Have you use this kind of method, or have you another more used ?[/code]