Announcement Announcement Module
Collapse
No announcement yet.
LazyInitializationException in init-method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • LazyInitializationException in init-method

    Hello All.

    It's again about LazyInitializationException.

    I have method in service class:
    Code:
    @Transactional(readOnly=true)
    public void init() {
    	List<Auction> auctions = getAuctionDao().findByStatus(AuctionStatus.ACTIVE.toString());
    	AuctionSummary as = new AuctionSummary(auctions.get(0));
    	as.setLotName(auctions.get(0).getLot().getName());
    	logger.info(as.getLotName());
    }
    This works fine, but if I mark this method as init-method in spring config, I have got LazyInitializationException.

    My question is does Hibernate session is open during init method?
    And if not why?

    Thanks a lot.

  • #2
    The method is not executed in transactional context when it's called as a part of the bean initialization. So, the hibernate stuff works at 'autocommit' mode and that is the most probable reason of the incorrect behavior.

    Solution is to exploit aspectj weaving for tx logic injection.

    Comment


    • #3
      Thanks a lot. It's working. I add to my config:
      Code:
      <context:load-time-weaver/>
      <tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>

      Comment

      Working...
      X