Results 1 to 3 of 3

Thread: creating a new object with safe care

  1. #1
    Join Date
    Aug 2004
    Posts
    4

    Default creating a new object with safe care

    Hi guys,
    My DAO has the following methods:
    Code:
      boolean isAccountExist(Account account);//check if specified account exist in database
      void createAccount(Account account); //save the specified Account object via saveOrUpdate
    In the service layer I would like to have method creating a new account but only when the account doesn't exist yet; something like that:

    Code:
    public void createAccountService(Account account)
    {
      if(!dao.isAccountExist(account))
      {
       //***1***
        //other user can create the same account(with the same login name)
        dao.createAccount(account);
      }
    }
    The question is:how can I guarantee that between checking if account exist and creating the account in database (the ***1*** line in the above code snippet), no one create the same account (with the same login name)? I know that I must use transaction, but could you explicite show me how to configure it, please?


  2. #2
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    Your service layer should be already wrapped transactionally, first of all.

    My strategy in the past has been to have the service layer code do a programatic check (via a query) for a duplicate existing username, email, etc.. This throws a checked exception, from which the ui can reover by letting the user type another value, and the service code can throw a different checked exception for each value which is being checked for dupes. Now of course, it is still possible that by the time you commit, anothr thread has inserted a record which would conflict. At that point, I am going to get a DataIntegrityViolationException. I consider this such a rare and unlikely case that I just let this go up as-is to the view layer, to be handled by a general RuntimeException handler there. Depending on the actual usage scenario of the app in question, this may not be a good enough strategy. The service layer could certainly just try to do a commit and catch the DataIntegrityViolationException itself, and then throwing a more specific checked exception.

    Regards,
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  3. #3
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    Just a small addition,

    You need to create a unique index on your column (login name) to ensure the DataIntegrityViolationException will be thrown!!!

    I also use the same strategy as Colin, this way, you can create new objects using a single round-trip to your database, and let your rdbms check if the record already exist.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •