-->
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: Concurrent exception ...
PostPosted: Thu Mar 12, 2009 10:06 am 
Newbie

Joined: Tue Dec 23, 2008 10:15 pm
Posts: 3
I'm using Hibernate with HSQL database in a Swing application.
For representing data JTable is used with custom table model. The main entity in database is a Movie, and for getting a Movie there is a method:

Code:
public static Movie getMovie(Integer movieId) {
      EntityManager em = DatabaseManager.getEntityManager();
      return em.find(Movie.class, movieId);
}


So, table model also uses this method for filling the table. Here is how that table model looks:

Code:
public class MovieTableModel extends AbstractTableModel {
   
   private List<Integer> idList;

   public MovieTableModel() {
    EntityManager em = DatabaseManager.getEntityManager();
    Query query = em.createQuery("select m.id from Movie m");
      idList = query.getResultList();
   }
   
   ...

   @Override
   public Object getValueAt(int rowIndex, int columnIndex) {
      Object result = null;

      Movie movie = MovieManager.getMovie(idList.get(rowIndex));
      
      if (movie == null)
         return null;

      switch (columnIndex) {
         case 1:
            result = movie.getTitle();
            break;
         case 2:
            ...
      }

      return result;
   }
}


The problem is when getMovie method is called from two threads, for example from EDT when repainting table and SwingWorker that is used to export data to e.g. PDF ... and I get ConcurrentModificationException or NullPointerException.

Any suggestion how to solve this ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 2:31 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I would guess that an EntityManager is using a Hibernate Session under the hood. And that mean that it is not thread-safe. So, if you have multiple threads you probably also need a different EntityManager for each thread. Now, I don't know anything about the best way to implement this in a Swing app (I am only working with web apps), but there is a discussion about this on the Hibernate wiki. See http://www.hibernate.org/333.html


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.