开发者

Another LazyInitializationException (in combination with Spring+GSON)

I guess I'm another newbie guy who fails to understand Hibernate sessions, may be Spring's TransactionTemplate, dunno. Here's my story.

I'm using Hibernate 3.5.5-Final, Spring 3.0.4.RELEASE, trying to live only with annotations (for Hibernate as well as Spring MVC).

My first try was to use @Transactional annotations in combination with properly setup transaction manager. Seemed to work at first, but in long run (about 36hours) I started to receive "LazyInitializationExceptions" over and over again (from places that were running just fine in previous hours!).

So I switched to manual transactions using Spring TransactionTemplate.

Basically I'm having something like this protected stuff in my BaseService

@Autowired
protected HibernateTransactionManager transactionManager;

protected void inTransaction(final Runnable runnable) {
    TransactionTemplate transaction = new TransactionTemplate(transactionManager);
    transaction.execute(new TransactionCallback<B开发者_运维百科oolean>() {    
        @Override
        public Boolean doInTransaction(TransactionStatus status) {
            try {
                runnable.run();
                return true;
            } catch (Exception e) {
                status.setRollbackOnly();
                log.error("Exception in transaction.", e));
                throw new RuntimeException("Exception in transaction.", e);
            }
        }
    });
}

And using this method from the service's impls worked OK, I did not see LazyInitializationException for 10 days (running Tomcat with this single app 24*7 for 10days, no restarts) ... but than hoops! It has popped again :-/

The LazyInitializationException comes from place under "inTransaction" method and there is no "inTransaction recursion" involved, so I'm pretty sure I should be in the same transaction alas in the same Hibernate session. There is no "data from previous session" involved (as far as my code goes that service layer opens the transaction, gathers all data from Hibernate it needs, process it and returns some result == service does not recall other top-services)

I have not profiled my app (I don't even know how to do that properly in long runs such as 10 days), but my only guess is that I'm leaking memory somewhere and JVM hits heap-limit... Is there some "SoftReferences" involed inside Spring or Hibernat? I don't know...

Another funny thing is that the exception always happen when I try to serialize the result into JSON using Google GSON serializer. I know, it does not use getters ... I have my own patched version that is using getters instead of actual fields (so I'm making sure not to bypass Hibernate proxy mechanisms), do you think it may play some role here?

Last funny thing is that the exception is always happing in the single service method (not anywhere else), which is driving me nuts because this method is as simple-stupid as it could be (no extrem DB operations, just loads data and serialize them to JSON using lazy-fetching), huh???

Does anybody have any suggestions what should I try? Do you have some similar experiences?

Thanks, Jakub

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜