Results 1 to 3 of 3

Thread: init-method comes before @Transactional

  1. #1
    Join Date
    Apr 2010
    Posts
    24

    Default init-method comes before @Transactional

    Hey there, I have a class with a public @Transactional method which loads some values from the DB.

    I'm declaring the bean in the xml with the init-method argument set.

    The problem I'm facing is that it seams that the init-method gets called before the bean is proxified because of the @Transactional. So it's failing because Hibernate's losing the connection.

    Is this correct ? I thought that init-method was called after "everything" was ready. Including the proxy.

    If so, how do I change the order so that init-method calls the proxified bean and not the vainilla implementation?

    PS: yes, Spring AOP is configured correctly and it works OK if I call the init-method from outside (after the bean has been created completely) and not from the bean declaration itself.

    Thank you!

    Fede.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Is this correct ? I thought that init-method was called after "everything" was ready. Including the proxy.
    You are wrong... the init method is called after construction and after all the properties have been set.. So BEFORE the proxies are created. If you want to make it transactional you will have to do it yourself by using for instance a TransactionTemplate inside your method... If all you want is to setup some data create an ApplicationListener which listens for ContectRefreshed events and when received initialize your data...

    BTW this question has been answered numerous times before so I also suggest the search....
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    FYI, the best answer I've found to this is here:
    http://forum.springsource.org/showth...167#post138167
    Just put your transactional logic in a service, and call it from your init method.

Tags for this Thread

Posting Permissions

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