Hi,
I am having a problem using Hibernate with Spring-managed sessions within a threaded object.
I have a class that extends Runnable that I use to read byte arrays from a socket that's connected to an external system. It is instantiated using Thread.
I get a nullPointerException error when I try to get the Spring sessionFactory that I've injected into the class. Also, if I call my DAO objects to do database updates from within the Thread, I also get a nullPointerException for the Hibernate session.
My DAO classes work fine when I do not call them from a Thread. I use a separate session to avoid deadlock issues.
Starting thread:
MyThreadedClass is fairly big, so I will just post salient snippets:Code:Thread thread = new Thread(new MyThreadedClass(param1, param2)); thread.start();
Please help !Code:@Repository public class MyThreadedClass implements Runnable { private SessionFactory sessionFactory; @Autowired public void setSessionFactory(SessionFactory factory) { sessionFactory = factory; } @Transactional public void updateDatabase() { Session mySession = sessionFactory.openSession(); Transaction tx = null; try { tx = mySession.beginTransaction(); } catch (HibernateException e1) { logger.error("!!!!!!!!! booHooHoo failed to start transaction : {}", e1); } try { mySession.update(myTable); tx.commit(); } catch (HibernateException e) { logger.error("!!!!!!!!! booHooHoo update of myTable failed : {}", e); }


Reply With Quote
