0

ZK, Spring Transaction Boundaries and JPA

asked 2007-09-20 18:28:16 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4529159

By: nwakefield

Hi,

Looking for some advice on what people think is the best way to implement transaction boundaries in the following scenario.

We are using a TreeModel which points to an underlying Spring/JPA managed POJO that contains child POJO's via a list mapped using one-to-many relationships
- nothing unusual. Each POJO has "List getChildren()" method which returns the contained collection and therefore forms a tree structure.

As the TreeModel loads child items on demand we get the following error "failed to lazily initialize a collection" as we expand the tree.

This is due to the collection contained within the POJO being called outside of a transaction/session.

Therefore has anyone tried wrapping the whole tree, page or onClick (expand) event in a transaction or should I go about it in a different way?

I've read the various spring articles on the ZK site and tried several approaches but has no luck so far.

TIA

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2007-09-21 01:54:34 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4529893

By: jebberwocky

Hello Tia

Did you open & close a connection to database, whenever u need accessing the data?
FYI:
What treemodel & the treerender do is getting data from the "tree" structure and render its data into corresponding ZK tree construction. In the general, how treemodel accesses the data from database, objects, or arrays is depended on the implementation.

Jeff

link publish delete flag offensive edit

answered 2007-09-21 04:44:14 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4530018

By: henrichen

Hi nicholas,

Did you use the "OpenSessionInView" pattern in Spring?

/henri




link publish delete flag offensive edit

answered 2007-09-21 07:18:24 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4530155

By: sousa1981

hehehehehehe, the world of PERSISTENCE.

<!-- Hibernate OpenSession Filter -->
<filter>
<filter-name>lazyLoadingFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>lazyLoadingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

I think is not good idea use OpenSessionInViewFilter.

The better idea is make it managed by your Manager. Here you will never going have problems.

Maybe you post your code to us help you :-)

Regards,

Marcos de Sousa

link publish delete flag offensive edit

answered 2007-09-21 15:37:01 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4530800

By: nwakefield

I'll see if I can post my code but it's going to be a little difficult due to all the layers.

Opening and closing the db connection every time works for objects which don't contain child objects through lazy loading, as the objects are fully instantiated and attachment isn't really important. However lazy loading with JPA requires the object to be attached and once it becomes detached you get the above problem.
In theory the filter should work and I'll see if that solves my problem but I think I tried it and had some problem, maybe I had it wrong.

I think the bigger question is how does one manage transaction boundaries with AJAX rich client interfaces. Consider a wizard style component, which configures a set of POJO's through a series of questions and answers. Using the wizard to create the POJO's is simple, create them, answer the questions and then save them but what happens when you want to modify them.

Ideally you would begin a transaction, select the POJO's, change the question answers, update the POJO in JPA and commit the transaction. This supposedly can be done using Extended Persistence Contexts in Spring but I would like to know if someone has done this, if not and I'll volunteer to write it up.

TIA = Thanks in advance.

link publish delete flag offensive edit

answered 2007-09-21 17:28:48 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4530945

By: sousa1981

Using Request Based like JSF/STRUT or component based like Wicket/etc is same if you use RIA like ZK Framework, so my advice is:
"The better idea is make it managed by your Manager. Here you will never going have problems."

The problem of lazy loading is not because the "AJAX rich client interfaces".
This problem is CLASSIC when you use PERSISTENCE under Web Applications.

If you have problem in View Layer simple use Hibernate OpenSession Filter, this is enough to resolve he problem.

You could send me your project with step by step installation and future explanation, I will analyze soon as possible.

So contact me (send the project) at sousa1981 AT yahoo DOT com DOT br

Regards,

Marcos de Sousa

link publish delete flag offensive edit

answered 2007-09-21 18:52:26 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4531044

By: matteo_barbieri

Hi, I faced the same problem some time ago.
I use Hibernate/Spring, and OpenSessionInViewFilter does not solved all my problems.

Sessions were properly opened/closed by the filter, but proxied objects were not reattached to the current session, causing lot of LazyInitializationException when I tried to access collections associated with an object retrieved in a previous session.

Instead of manually reattach objects before accessing its collections I do the reattach with an aspect (using AspectJ).

OpenSessionInViewFilter correctly opens and closes connection but I had to manage detached objects this way.
Maybe there are better solutions, but with my little experience I'm not able to find one...

Does anybody faced the same problem?

If needed I could give a longer explaination on what the aspect do, but it mainly try to get the collection, and if the exception is thrown, a reattach is done.

Bye

link publish delete flag offensive edit

answered 2007-09-22 08:52:22 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4531811

By: sousa1981

"The better idea is make it managed by your Manager. Here you will never going have problems."

Yes, using Spring AOP (AspectJ) is the solution to the problem, since we could decorate an method with transaction boundary.

Regards,

Marcos de Sousa

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2007-09-20 18:28:16 +0800

Seen: 440 times

Last updated: Sep 22 '07

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More