Results 1 to 5 of 5

Thread: Downgrading RuntimeException to Checked Exception

  1. #1

    Default Downgrading RuntimeException to Checked Exception

    I use Hibernate as the persistence mechanism. Let's say I have a transaction that is dependent upon another persistent object which is loaded during that transaction. In the "load" method for the child object we use something like this:
    Code:
        public User getUser(String username) {
            User user = (User) getHibernateTemplate().get(User.class, username);
            if (user == null) {
                throw new ObjectRetrievalFailureException(User.class, username);
            }
            return user;
        }
    Since the ObjectRetrievalFailureException is a runtime exception, Spring automagically rolls back the transaction. I realize I could just return null from the getUser method or return a checked exception.. but what if I don't want to for whatever reasons. Is there any way for me to downgrade the RuntimeException to a checked Exception so that I can declaratively determine whether or not to rollback?

    -Matt

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

    Default

    Using Checked Exception willl require you to change the method declaration (add a throw) and use a try / catch whenever you call your method. A better solution would be to use the -/+ ObjectRetrievalFailureException in the transaction attribute for your method.

    HTH
    Omar Irbouh

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

  3. #3
    Join Date
    Aug 2004
    Location
    Tampa, FL
    Posts
    39

    Default

    I'm not sure I understand what you want to do. Do you want to throw the exception but still commit the transaction? In that case, here are something that may help you:

    http://www.springframework.org/docs/...n.html#d0e4693
    You can specify what behavior exceptions cause declaratively (both checked and unchecked). So for a particular method, you could say that ObjectRetrievalFailureException causes commit. For example:

    Code:
    <prop key="getUser">PROPAGATION_REQUIRED,+ObjectRetrievalFailureException</prop>
    Nilesh Kapadia
    http://www.nileshk.com

  4. #4

    Default Strange Behavior

    I tried to specify +Exception as a test (before I did my first post) and it still rolled the exception back. Do I have to specify the exact Exception? I thought any subclass would be honored... maybe I should get off the crack pipe. I will try it again to see if that works.

    -Matt

  5. #5
    Join Date
    Apr 2005
    Posts
    17

    Default

    <prop key="getUser">PROPAGATION_REQUIRED,+ObjectRetrieva lFailureException</prop>
    None of the examples I have seen on this uses a package name prefix on the exception. Is this correct? I thought I had to use:

    Code:
    <prop key="getUser">PROPAGATION_REQUIRED,+com.example.ObjectRetrievalFailureException</prop>
    Is it just that exceptions in examples tend to be declared in the default package?

Similar Threads

  1. Context initialization failed
    By kanonmicke in forum Container
    Replies: 7
    Last Post: Sep 29th, 2005, 12:35 AM
  2. Replies: 0
    Last Post: Jul 11th, 2005, 05:49 PM
  3. Replies: 3
    Last Post: Feb 18th, 2005, 04:41 PM
  4. Replies: 3
    Last Post: Nov 8th, 2004, 07:30 PM
  5. Checked vs unchecked exception
    By scesbron in forum Architecture
    Replies: 2
    Last Post: Sep 10th, 2004, 01:30 AM

Posting Permissions

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