-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: records being deleted without the delete method
PostPosted: Fri Oct 08, 2004 2:29 pm 
Newbie

Joined: Mon Sep 20, 2004 4:17 pm
Posts: 3
Hibernate version: 2.1.6
Mapping documents:

Code between sessionFactory.openSession() and session.close():
public class HibernateUtil {

/** All our threads can share one constant */
private static SessionFactory sessionFactory= null;

/** the session of the current thread */
private static final ThreadLocal threadSession = new ThreadLocal();

/** one database transaction for all operations */
private static final ThreadLocal threadTransaction = new ThreadLocal();

/** session */
private static Session session;

static {
try {
Configuration cfg = new Configuration();
sessionFactory = cfg.configure().buildSessionFactory();

} catch (Throwable ex) {
ex.printStackTrace(System.out);
throw new ExceptionInInitializerError(ex);
}

}

/**
* gets a session that is extended to use the local thread variable
* @return
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {

// Configuration cfg = new Configuration();
// sessionFactory = cfg.configure().buildSessionFactory();

session = (Session) threadSession.get();
try {
if (session == null) {
session = sessionFactory.openSession();
}
} catch (HibernateException ex) {

}
return sessionFactory.openSession();

}

/**
* closes the current session
*
*/
public static void closeSession() {
try {
//Session s = (Session) threadSession.get();
//threadSession.set(null);
if (session != null || session.isOpen()) {
session.close();
sessionFactory.close();
}

} catch (HibernateException hE) {
System.out.println("Closing the session" + hE.toString());

}
}

/**
* This method starts a new database transaction
*
*/
public static void beginTransaction() {
Transaction tx = (Transaction) threadSession.get();
//Transaction tx = null;
try {
if (tx == null) {
tx = getSession().beginTransaction();
threadTransaction.set(tx);
}
} catch (HibernateException hE) {
// TODO: handle exception
}
}

/**
* This method commits transaction if fails immediately rolls back the transaction
*
*/
public static void commitTransaction() {
Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
tx.commit();
threadTransaction.set(null);

}
} catch (HibernateException hE) {

}
}
/**
* This method closes the session after the transaction rollback
*
*/
public static void rollbackTransaction(){
Transaction tx = (Transaction)threadTransaction.get();
//Transaction tx = null;
try{
threadTransaction.set(null);
if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack()){
tx.rollback();
}
tx.rollback();
} catch(HibernateException hE){
} finally{
closeSession();
}

}


it saves find but when I restart the server the records in the database are lost. Once the records are in the database how are they being deleted if server being restarted. I am not using the delete method.



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:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 08, 2004 2:48 pm 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
Check the records are in the db after this session is closed, it may be you are not committing properly.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.