-->
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.  [ 3 posts ] 
Author Message
 Post subject: Transaction problem Jboss + hibernate
PostPosted: Tue Dec 07, 2004 3:56 am 
Newbie

Joined: Tue Dec 07, 2004 3:40 am
Posts: 2
Hibernate version:2.1.7c

Mapping documents:

Code between sessionFactory.openSession() and session.close():see below

Full stack trace of any exception that occurs:

Name and version of the database you are using:PostgreSQL 7.3.1 windows

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

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:


Code:

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
Code:

<?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:

Code:

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?


Top
 Profile  
 
 Post subject: Re: Transaction problem Jboss + hibernate
PostPosted: Tue Dec 07, 2004 5:13 am 
Beginner
Beginner

Joined: Tue Oct 26, 2004 12:45 pm
Posts: 43
Location: Israel
marek wrote:
hibernate.cfg.xml
Code:

<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>




Maybe (only maybe) the problem is in the configuration file.
you make hibernate create a jdbc SessionFactory (not container datasource) and also define it to use JTA (which is container managed transaction).

My suggestion is to remove the the underlined rows, and try again.
Alternatively, you can use a datasource and not connection url.

again, maybe i m right, maybe not.

Jus.


Top
 Profile  
 
 Post subject: Re: Transaction problem Jboss + hibernate
PostPosted: Tue Dec 07, 2004 6:28 am 
Newbie

Joined: Tue Dec 07, 2004 3:40 am
Posts: 2
Thank you I'll try this


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.