Hi everybody! I want to ask for your help about using Hibernate to persist an object. There's an error when I try to make some updates. It says that there's a synchronization error with the database o something like that. I think that I'm not using Hibernate or the Session object in a good way. So I want to show you my code to help me to fix it. Is it correct to open a Session every time? Well, I need your help to solve this. Thanks in advance!

import com.business.entities.Dose;
import com.presentation.DateHandler;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

public class DoseHandler {
private SessionFactory sessionFactory;

private DateHandler dateHandler;

public DoseHandler(SessionFactoryHandler sessionFactoryHandler) {
this.sessionFactory = sessionFactoryHandler.getSessionFactory();
dateHandler=new DateHandler();
}

public Integer saveDose(Dose dose) {
try {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Serializable serializable=session.save(dose);
tx.commit();
session.close();
return new Integer(serializable.toString());
} catch (HibernateException e) {
e.printStackTrace();
return -1;
}
}

public void deleteDose(Integer id) {
try {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Dose dose = (Dose)session.load(Dose.class,id);
session.delete(dose);
tx.commit();
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}

public void updateDose(Integer id,Dose newDose) {
try {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

Dose dose = (Dose)session.load(Dose.class,id);

dose.setDoseDate(newDose.getDoseDate());
dose.setAuthorizationNumber(newDose.getAuthorizati onNumber());
dose.setEmissionLimitDate(newDose.getEmissionLimit Date());
dose.setDoseKey(newDose.getDoseKey());
dose.setInvoiceNumberStart(newDose.getInvoiceNumbe rStart());
dose.setActive(newDose.getActive());

session.update(dose);
tx.commit();
session.close();//there's an error when a new dose is added
} catch (HibernateException e) {
e.printStackTrace();
}
}

public Dose getDose(Dose dose) {
try {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
String query="from Dose d where d.doseId="+dose.getDoseId();
List result = session.createQuery(query).list();
tx.commit();
session.close();
if(result.isEmpty()) return null;
return (Dose)result.get(0);
} catch (HibernateException e) {
throw new RuntimeException(e.getMessage());
}
}

public Dose getActiveDose() {
try {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
String query="from Dose d where d.active=1";
List result = session.createQuery(query).list();
tx.commit();
session.close();
if(result.isEmpty()) return null;
return (Dose)result.get(0);
} catch (HibernateException e) {
throw new RuntimeException(e.getMessage());
}
}

public long getRemainDays() {
try {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
String query="from Dose d where d.active=1";
List result = session.createQuery(query).list();
tx.commit();
session.close();
if(result.isEmpty()) return -1;
Dose d=(Dose)result.get(0);
Calendar limitDay=Calendar.getInstance();
limitDay.setTime(d.getEmissionLimitDate());
Calendar today=Calendar.getInstance();
today.setTime(new Date());
return (limitDay.getTimeInMillis()-today.getTimeInMillis())/86400000+1;
} catch (HibernateException e) {
throw new RuntimeException(e.getMessage());
}
}

}