|
Hi, I'm developing a standAlone application wich will run on several machines each one a particula instance of it. Each instance access the same and Unique MySql server wich is running on one of these machines. The application uses Hibernate to map the data from/to Database.
The problem is that when I persist an object using the saveOrUpdate method drawed below, the new instance is refreshed only on the machine where change was processed, the others machines don't see the change until the application is restarted. I 've tried to avoid caching queries modifying the properties files but I wasn't lucky.
Below there is two method I use to save or Update objects, and to read a list of objects depending upon a dYnamic query with certain quantity of results permitted.
I put the useful class I use to get and close hibernate sessions.
Thanks In Advance, I'd appreciate any clues.
Mauro Pisano.
Hibernate version:2.1.3
Mapping documents:
Code between sessionFactory.openSession() and session.close():
public void saveOrUpdate(Object obj) {
Session session;
Transaction tx = null;
try {
session = HibernateUtil.currentSession();
tx = session.beginTransaction();
session.saveOrUpdate(obj);
tx.commit();
} catch (Exception e) {
try {
if (tx != null) {
tx.rollback();
}
} catch (Exception e2) {
System.out.println("Exception 2");
}
} finally {
try {
HibernateUtil.closeSession();
} catch (Exception e) {
System.out.println( "Exception Closing session");
}
}
}
public List listByCriteria(String str, int maxResult) {
Session session;
List results=null;
try {
session = HibernateUtil.currentSession();
Query qry = session.createQuery(str);
if (maxResult > 0) {
qry.setMaxResults(maxResult);
}
qry.setCacheable(false);
results = qry.list();
} catch (Exception e) {
System.out.println("Exception)");
} finally {
try {
HibernateUtil.closeSession();
} catch (Exception e2) {
System.out.println("Exception closing session)");
}
return results;
}
}
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ThreadLocal session;
static {
try {
// Create the SessionFactory
sessionFactory =
new Configuration().configure().buildSessionFactory();
session = new ThreadLocal();
} catch (HibernateException ex) {
System.out.println("Exception on hibernateUtil");
ex.getCause();
ex.getMessage();
throw new RuntimeException( "Configuration problem: " + ex.getMessage(),ex);
} catch (ExceptionInInitializerError e2) {
System.out.println("Exception2 on hibernateUtil");
e2.getCause();
e2.getMessage();
}
}
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}
}
Full stack trace of any exception that occurs:
Name and version of the database you are using:
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
|