-->
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.  [ 1 post ] 
Author Message
 Post subject: Question about locking database table
PostPosted: Fri Jun 19, 2015 1:33 am 
Newbie

Joined: Fri Jun 19, 2015 12:44 am
Posts: 1
Hi! I need help. I'm just starting to learn hibernate.

Used DBMS - MySQL.
This code reads an entry from view "FreeGroup" and writes the value in the table "BlockedGroup". Several threads have simultaneous access to this code.
What can i do to lock in the reading of "FreeGroup" and at the same time lock in the writing table "BlockedGroup"?

Code:
public Long addBlockedGroup() throws SQLException {
      Long groupId = (long) -1;
      Session session = null;
      try {
         session = HibernateUtil.getSessionFactory().openSession();
         
         session.beginTransaction();      
         List<FreeGroup> freeGroups = new ArrayList<FreeGroup>();
         
         freeGroups = session.createQuery("SELECT i FROM FreeGroup i").setLockMode("i", LockMode.READ).list();
               session.createCriteria(FreeGroup.class).setMaxResults(1).list();         
         
         if (freeGroups.size() != 0) {
            groupId = freeGroups.get(0).getGroupId();
            BlockedGroup blockedGroup = new BlockedGroup(groupId);
            session.save(blockedGroup);         
         }
         session.getTransaction().commit();   
         
      } catch (Exception e) {
         System.err.println("AddBlockedGroup failed: " + e.getMessage());
      } finally {
         if (session != null && session.isOpen()) {
            session.close();
         }
         return groupId;
      }
   }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.