Hi,

 we started to use Jboss 3.2.6 with hibernate 2.1.7c. Everything is working 
fine except that no data are persistened into database upon commint neither 
when shutting down jboss. Here is the code snippet using ThreadLocal object to 
hold sessions and transactions:
 


  | private static ThreadLocal threadSession = new ThreadLocal();
  | 
  | private static ThreadLocal threadTransaction = new ThreadLocal();
  | 
  | private static SessionFactory sessionFactory;
  | 
  | static {
  |  try  {
  |            sessionFactory = new 
Configuration().configure().buildSessionFactory();
  |        } catch (Throwable e) {
  |     e.printStackTrace();
  |     throw new ExceptionInInitializerError(e);
  |        }
  |       }
  | 
  | public Object initSession() throws Exception {
  |    Session session = (Session) threadSession.get();
  |     try {
  |            if (session == null) {
  |                 session = sessionFactory.openSession();
  |                             threadSession.set(session);
  |     System.out.println("Session # " + session.toString() + " opened");
  |           }
  |     if (!session.isConnected()) {
  |         session.reconnect();
  |         System.out.println("Session # " + session.toString() + " 
reconnectd");
  |                 } 
  |     } catch (HibernateException e) {
  |       e.printStackTrace();
  |     throw e;
  |     }
  |        return session;
  |     }
  | 
  | protected Session getSession() throws Exception {
  |     Session session = (Session) threadSession.get();
  |     try {
  |          if (session == null){
  |     session = sessionFactory.openSession();
  |     threadSession.set(session);
  |     } else if (session != null && !session.isOpen()){
  |     session = sessionFactory.openSession();
  |     threadSession.set(session);
  |     Transaction tx = (Transaction)threadTransaction.get();
  |        if (tx != null){
  |            commitTransaction();
  |        }
  |     }
  |     } catch (HibernateException ex) {
  |        throw new Exception(ex);
  |     }
  |     return session;
  |    }
  | 
  | public void closeSession() throws Exception {
  |     Session session = (Session) threadSession.get();
  |     try {
  |                    threadSession.set(null);
  |            if (session != null && session.isOpen()) {
  |               session.flush();
  |               System.out.println("Closing session # " + session.toString());
  |             session.close();
  |     }
  |     } catch (HibernateException e) {
  |        e.printStackTrace();
  |        throw e;
  |     }
  |            }
  | 
  | public void beginTransaction() throws Exception {
  |               Transaction trans = (Transaction)           
threadTransaction.get();
  |     try {
  |            if (trans == null) {
  |     trans = ((Session) getSession()).beginTransaction();
  |     threadTransaction.set(trans);
  |     }
  |     } catch (HibernateException e) {
  |        e.printStackTrace();
  |        throw e;
  |     }
  |       }
  | 
  | public void commitTransaction() throws Exception {
  |     Transaction trans = (Transaction) threadTransaction.get();
  |     try {
  |            if (trans != null && !trans.wasCommitted() && 
!trans.wasRolledBack()) {
  | //                          getSession().flush();
  | // Connection commit works but we could not use it in transaction 
environment                               
  | //  getSession().connection().commit();
  |             trans.commit();
  |             getSession().close();
  |             threadTransaction.set(null);
  |             threadSession.set(null);
  |       }
  |     } catch (HibernateException e) {
  |                     rollbackTransaction();
  |             getSession().flush();
  |             getSession().close();
  |             threadSession.set(null);
  |             e.printStackTrace();
  |             throw e;
  |     }
  |             }
  | 
  |   public void rollbackTransaction() throws Exception {
  |       Transaction trans = (Transaction) threadTransaction.get();
  |     try {
  |            threadTransaction.set(null);
  |            if (trans != null && !trans.wasCommitted() && 
!trans.wasRolledBack()) {
  |             trans.rollback();
  |     }
  |     } catch (HibernateException e) {
  |                     e.printStackTrace();
  |                     throw e;
  |             } finally {
  |                     closeSession();
  |             }
  |     }
  | 
  |     public Object saveObject(Object object) throws Exception {
  | //          getSession().saveOrUpdate(object);
  |             getSession().save(object);
  |             return object;
  |     }
  |     
  |     
  | 
  |     public Object updateObject(Object object) throws Exception {
  | //##                return saveObject(object);
  |                     getSession().saveOrUpdate(object);
  |                     return null;
  |     }
  |     
  |     
  |     public void deleteObject(Object obj) throws Exception {
  |             Session ses = getSession();
  |             ses.delete(obj);
  |     }
  | 
  |     public Collection findObjects(String query) throws Exception {
  |             return getSession().find(query);
  |     }
  | 
  |     public Collection findObject(String query, Object obj, Object type) 
throws Exception {
  |             Type htype = (Type) type;
  |             return getSession().find(query, obj, htype);
  |     }
  | 
  |     public Collection findObjects(String query, Object[] objs, Object[] 
types) throws Exception {
  |             Type[] htypes = (Type[]) types;
  |             return getSession().find(query, objs, htypes);
  |     }
  | 
  |     public Collection findObjects(String query, Map parameters)     throws 
Exception {
  |             Query q = getSession().createQuery(query);
  | 
  |             Iterator iter = parameters.keySet().iterator(); 
  |             while(iter.hasNext()) {
  |                     String key = (String) iter.next();
  |                     q.setParameter(key, parameters.get(key));
  |             }
  | 
  |             return q.list();
  |     }
  | 
  |     public Object findById(Class cl, long id) {
  |             Object obj = null;
  |             
  |             try {
  |                     Long newId = new Long(id);
  |                     obj = getSession().get(cl, newId);
  |             } catch (Exception e) {
  |                     e.printStackTrace();
  |             }
  |             return obj;
  |     }
  | 
  |     public Collection findAllObjects(Class voClass) throws Exception {
  |             Collection result = null;
  |             Session ses = getSession();
  |             if (ses.isOpen()) {
  |                     System.out.println("Session is open");  
  |             } else {
  |                     System.out.println("Session is closed");
  |             }
  |             result = ses.find("from " + voClass.getName());
  |             closeSession();
  |             return result;
  |     }
  | 
  | 

hibernate.cfg.xml
 
  | <?xml version="1.0" encoding="utf-8"?>
  | <!DOCTYPE hibernate-configuration
  |     PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
  |     "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd";>
  | 
  | <hibernate-configuration>
  |     <!--session-factory name="java:comp/env/jndi/HibernateSessionFactory"-->
  |     <session-factory>
  |             <!-- local connection properties -->
  |             <property 
name="hibernate.connection.url">jdbc:postgresql://localhost/test</property>
  |             <property 
name="hibernate.connection.driver_class">org.postgresql.Driver</property>
  |             <property name="hibernate.connection.username">test</property>
  |             <property name="hibernate.connection.password">test</property>
  | 
  |             <!-- transaction configuration -->
  |         <property 
name="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JTATransactionFactory</property>
  |         <property 
name="hibernate.transaction.manager_lookup_class">net.sf.hibernate.transaction.JBossTransactionManagerLookup</property>
  | 
  |             <!-- DB pooling configuration -->
  |             <property name="hibernate.c3p0.minPoolSize">5</property>
  |             <property name="hibernate.c3p0.maxPoolSize">20</property>
  |             <property name="hibernate.c3p0.timeout">1800</property>
  |             <property name="hibernate.c3p0.max_statement">50</property>
  | 
  |             <!-- dialect for PostgreSQL -->
  |         <property 
name="dialect">net.sf.hibernate.dialect.PostgreSQLDialect</property>
  |         <property name="hibernate.show_sql">true</property>
  | 
  | <!--        <property name="hibernate.use_outer_join">true</property> -->
  |       
  | <mapping resource="/blabla/UMUser.xml" /> 
  | 
  |     </session-factory>
  | </hibernate-configuration>
  | 
  | 

we are using stateless session beans this way:


  | public long createUser(UserDTO dto) {
  |             long result = OPERATION_ERROR;
  | 
  |             try {                   
  |                     UserManagementDAO dao = new UserManagementDAO();
  | 
  |                     dao.initSession();
  |                     dao.beginTransaction();
  |                     
  |                     // find user by login (unique name)
  |                     UMUser user = 
(UMUser)dao.findUserByLogin(dto.getLogin());
  |                     if (user != null) {
  |                             // error when login allready exists is database
  |                             log.error("[createUser]: login already exists");
  |                             return result;
  |                     }
  |                     
  |                     // create user value object from user dto
  |                     user = new UMUser();
  |                     user.setLogin(dto.getLogin());
  |                     user.setPassword(dto.getPassword());
  |                     user.setUserName(dto.getUserName());
  |                     
  |                     // save user into database
  |                     user = (UMUser) dao.saveObject(user);
  | 
  |                     dao.commitTransaction();
  |                     dao.closeSession();
  |                     
  |                     // return userId
  |                     result = user.getId();
  |                     
  |             } catch (Exception e) {
  |                     log.error("[createUser]: ", e);
  |             }
  |             return result;
  |     }
  | 
  |  

the data and changes are shown in presentation layer but never persisted into 
db. I'm almost mad about this spending last whole week to browsing newsgroups 
and forums searching for solution. Would be there any kindly help?

View the original post : 
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3857588#3857588

Reply to the post : 
http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=3857588


-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now. 
http://productguide.itmanagersjournal.com/
_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to