-->
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: Problem with transaction
PostPosted: Fri Jun 01, 2007 7:02 am 
Newbie

Joined: Fri Jun 01, 2007 6:13 am
Posts: 2
hi all,

I have a problem with transaction in hibernate because there is an impossibility of making a rollback.
I explain me : i have created a user class with hibernate annotations and i have made a test class in order to add user to my MySQL database.
The probleme is that when an user is already in my database, i would like the transaction be 'canceled', that is to say, the database return to its initial stat before inserting.

Code of user class :

Code:
@Entity
@Table(name = "USERS")
public class User {

   @Id
   @GeneratedValue
   @Column(name = "USER_ID")
   private Long id;

   @Column(name = "LOGIN", nullable = false, unique = true)
   private String login;

   @Column(name = "PASS", nullable = false)
   private String pass;

   @ManyToMany(targetEntity = com.calenco.users.Role.class, cascade = {
         CascadeType.PERSIST, CascadeType.MERGE })
   @JoinTable(name = "USER_ROLE", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "ROLE_ID") })
   private Collection<Role> roles = new ArrayList<Role>();

   @Column(name = "MAIL", nullable = false)
   private String mail;

   /**
    * Creates a new instance of Users
    */
   public User() {
   }

   public User(String login, String pass, String mail, Collection<Role> role) {
      super();
      this.login = login;
      this.pass = pass;
      this.roles = role;
      this.mail = mail;
   }

   public Long getId() {
      return id;
   }

   @SuppressWarnings("unused")
   private void setId(Long id) {
      this.id = id;
   }

   public String getLogin() {
      return login;
   }

   public String getPass() {
      return pass;
   }

   public void setLogin(String login) {
      this.login = login;
   }

   public String getMail() {
      return mail;
   }

   public Collection<Role> getRoles() {
      return roles;
   }

   public void setMail(String mail) {
      this.mail = mail;
   }

   public void setPass(String pass) {
      this.pass = pass;
   }

   public void setRoles(Collection<Role> roles) {
      this.roles = roles;
   }

   public void addRole(Role role) {
      roles.add(role);

   }

}


Code of test class :

Code:
public void addUser(User user) throws UserExistsException {
      if (userExists(user)) {
         throw new UserExistsException("Users already exists");
      } else {
         Session session = HibernateUtils.getSession();
         Transaction transact = session.beginTransaction();
         session.save(user);
         System.out.println("User added : " + user.getLogin());
         transact.commit();
         session.close();
      }
   }

   public boolean addUsers(List<User> users) throws UserExistsException {
      Session session = HibernateUtils.getSession();
      Transaction transact = null;

      boolean retVal = true;

      try {
         transact = session.beginTransaction();
         for (int i = 0; i < users.size(); i++) {
            System.out.println("Trying to add user " + i);
            addUser(users.get(i));
         }
         transact.commit();

      } catch (UserExistsException e) {
         if (transact != null)
            transact.rollback();
         retVal = false;
         throw e;

      } finally {
         session.close();
      }
      return retVal;
   }


I would like to know why my transaction doesn't execute a rollback.
If there is another method to do this, i would be happy to know it.

Thanks in advance for answers.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 01, 2007 8:19 am 
Newbie

Joined: Tue May 29, 2007 12:33 am
Posts: 13
Two options:

    (1)
    Code:
    Transaction transact = session.beginTransaction();

    and
    Code:
    transact.commit();

    and use that try-catch-finally block in code calling those methods.

    (2)
    Use transaction compensation to reverse committed inserts.

I would go with #1. The problem is that in
Code:
addUser(User user)
you are committing the transaction, so if an exception is thrown after inserting x number of users, those x number of users will get to be inserted in the database.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 01, 2007 8:23 am 
Newbie

Joined: Tue May 29, 2007 12:33 am
Posts: 13
Sorry, post went wrong, option #1 is

Factor out that code out of addUser and addUsers, and use that try-catch-finally block in code calling those methods.

Cheers,


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.