0

Hibernate and threads

asked 2008-09-11 10:48:35 +0800

Bentley gravatar image Bentley
18

updated 2008-09-11 15:12:10 +0800

Hi,

I am using Hibernate with ZK HibernateUtil to persist Objects. It works good for any HttpRequests. But for my application I need to start a thread that listens on a socket for incoming messages. These messages should also persist with Hibernate.

As the persist command is not called within an HttpRequest, the OpenSessionInViewListener doesn't work and it throws an exception that there is no active transaction.

Is there a solution to handle Hibernate commands with HibernateUtil within threads?

kind regards
Ben

Samplecode:

-----------
EntityA.java:
-----------
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class EntityA {

private int _id;

public EntityA() {}

@Id
public int getId() {return _id;}

public void setId(int id) { _id = id;}
}

-------------
EntityA_DAO.java:
-------------
public class EntityA_DAO {
org.hibernate.Session _session;

public EntityA_DAO() {
_session = HibernateUtil.getSessionFactory().getCurrentSession();
}

public void saveOrUpdate(EntityA newEntityA) {
_session.saveOrUpdate(newEntityA);
}
}

--------------
TestWindow.java
--------------
import org.zkoss.zul.Window;

public class TestWindow extends Window {

private Thread t;

public TestWindow(){
t = new Thread(new UpdateRunnable());
t.start();
}

class UpdateRunnable implements Runnable{

public void run() {
while(true){

EntityA a = new EntityA();

new EntityA_DAO.saveOrUpdate(a);
}
}
}
}

delete flag offensive retag edit

3 Replies

Sort by ยป oldest newest

answered 2008-09-12 01:38:51 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

updated 2008-09-12 01:39:14 +0800

since you create a thread yourself, why don't you just use you DAO in simple J2SE application way?
get the DataSource/Session/JDBC form spring/jind , open a Connection, sets the transaction, execute the sql , finally commit it and release the connection.

link publish delete flag offensive edit

answered 2008-09-12 09:15:54 +0800

Bentley gravatar image Bentley
18

because I wanted to avoid to create 2 DAOs for my Entities. One for ZK threads (using OpenSessionInViewListener) and another one for my own thread. But I guess that this is the only way...

thanks!

link publish delete flag offensive edit

answered 2008-09-15 12:35:05 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

Do you mean 'same session' ?
I don't think it is good idea if you use same session in two different asynchronized thread.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2008-09-11 10:48:35 +0800

Seen: 238 times

Last updated: Sep 15 '08

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More