-->
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: Transactional and Try-Catch
PostPosted: Wed Feb 02, 2011 5:44 am 
Newbie

Joined: Mon Jan 24, 2011 1:38 pm
Posts: 8
Hello together!
Ive got some questions to the structure of my hibernate-code and I would be glad if you could help me.
To show what I mean, I wrote some classes which clarify my problems in general. The questions are written down below.
Code:
public class Checks {

    private HibernateDao projectDao;

    @Transactional(readOnly = true)
    public Map startChecks(String a, String b) {

      // calls three checking methods, for example check1(), check2(), check3()
      // two of them are requesting data from the database and so are marked as @Transactional
      // depending on the result, a Model is composed and then returned
      
        return myModel;
    }

    @Transactional(readOnly = true)
    public String check1(String a) {
        // checks something by requesting data from the database
      // that means: calls method frm the class: HibernateDao
      // returns something, for example a String
    }
   

    public String check2(String a) {
        // checks something without requesting data from the database
      // returns something, for example a String
    }
   
   @Transactional(readOnly = true)
    public String check3(String a) {
        // checks something by requesting data from the database
      // that means: calls method frm the class: HibernateDao
      // returns something, for example a String
    }
}

Code:
public class CheckManager {

   private Checks checksClass;
   
   public void callChecks(){
      checksClass.startChecks();
   }
}

My first question is:
Why do I have to mark startChecks() as Transactional? If I dont, it is not working. I guess I must mark it as Transactional, because it calls Transactional-Methods.
What irritates me: If I would put startChecks() in another class, I would not have to mark it as Transactional - why??? (Like: callChecks() must not be marked as @Transactional)

My second question is:
The methods requesting the data in my HibernateDao-Class are not marked as Transactional, but the methods calling those HibernateDao-Methods are. Is that correct?

My third question is:
As Im new to Hibernate, Im not sure how to handle Exceptions. I created my HibernateDao that way, that - if there is no result - null is returned. So I can always use (if result==null ... else... etc)
Now I did not know exactly if I have to surround something by try-catch in case the database-query does not work (for example no connection or something like this - I dont know)

As my project is growing now I have various attempts for using try-catch:
1. I surround the calls of the HibernateDao-methods in check1() and check3().
2. I surround the calls of check1() and check3() in the method startChecks()
3. I surround the call of startChecks() in callChecks() from CheckManager

- I even combine those methods, for example 2. + 3. and I even go further and surround the call of callChecks in another method :-)
It seems pretty cumbersome to me. Do I have to use try-catch at all? And if yes, where do I have to put it?

It would be great if someone could help me, because Im new to it and Im a little bit confused. :-)


Top
 Profile  
 
 Post subject: Re: Transactional and Try-Catch
PostPosted: Sat Feb 05, 2011 1:56 pm 
Newbie

Joined: Fri Jan 07, 2011 7:23 am
Posts: 19
Hi,

1. Transactional is Spring related, not related to Hibernate, but anyway: when proxies are used to enable tx management, if you call such a proxy from "the outside" you get tx management, but if you call a method of the proxy from within the proxy itself (like your startChecks() calling check1()) you won't get any tx management, Spring is not involved at all.

2. depends on your architecture, both makes sense. I'm annotation all DAO methods with TX required and then I use an outer tx e.g. in controllers in order to have on tx spanning multiple DAO calls.

3. I hope you're using the "CheckManager" for testing purposes only! Normally I wouldn't check for no connection or such cases in single methods (like you wouldn't check for OutOfMemory in a normal method) Since you're using Spring you could easily map that exception independently from your code and/or use AOP.

regards,

Robin.


Top
 Profile  
 
 Post subject: Re: Transactional and Try-Catch
PostPosted: Fri Mar 04, 2011 7:01 am 
Newbie

Joined: Mon Jan 24, 2011 1:38 pm
Posts: 8
thanks for your answers!

Quote:
2. depends on your architecture, both makes sense. I'm annotation all DAO methods with TX required and then I use an outer tx e.g. in controllers in order to have on tx spanning multiple DAO calls.

so, in my architecture, Ive got simple Dao-methods which always have only one assignment. "Outside", in other classes, Ive got methods which always call some of the dao-methods. thats why I surround those "outside-methods" with @transactional, because one transaction = some dao methods, not one. do you think it makes sense? :-)

another question, Ive got one Dao-Class (hibernatedao). is it correct, that I only need ONE object (always the same instance) of it in a webapplication?? so I always use the same object, in different classes for different users? (there cant get anything mixed up?)

and...
Quote:
3. I hope you're using the "CheckManager" for testing purposes only! Normally I wouldn't check for no connection or such cases in single methods (like you wouldn't check for OutOfMemory in a normal method) Since you're using Spring you could easily map that exception independently from your code and/or use AOP.

whats wrong with me CheckManager? I dont understand :-( and what means "map exception idepedently from your code"? I mean, if I call a dao-method from ClassA:MethodA and ClassA:MethodA is called from ClassB:MethodB. ClassA:MethodA is marked then as @Transactional. Do I need a Try/Catch in ClassB:MethodB in case anything does go wrong and I do not get the obejct I expect to be returned? like I said, I check then if a object is returned or not, but do I have to check for any other exceptions? do I have to use try/catch? no - because I use @transactional, that is enough?

thank you for helping me understand! :-)

[btw, if anyone speaks german here - I would be glad to exchange experiences and informations with someone :-) its complicated for me in english, but anyway, Im happy for ANY answer! :-)]


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.