This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

@Transactional DAO and proxy-target-class

May 19th, 2009, 06:47 AM

Hi!

I have to say, I've been reading all the documentation and some forum posts, but I can't get things to work the way I'd like.

I'm new to Spring and I'm developing an application using jsf+spring+hibernate3. I was having all sorts of problems with "lazy inizialization" and "two open sessions". So I decided to use the Open Session in view filter. The I started getting some other error (now I can't recall), so that's when I read something about using annotations, the @Transactional.

It seemed a good idea so I added that to my DAO classes, but the thing is that I only get that to work if I add the proxy-target-class="true" in the config file, which I understand is not recommended, for what I can read on the Spring documentation. I would really appreciate if someone could point me out what I'm doing wrong, or tell me if that's not really a big problem.

My DAO structure is the following, I have a class called Notas (for example):

So basically I added the @Transactional to methods that need to write on the DB. At first so I could try this, I just added the @Transactional to the update methods, but it required proxy-target-class set to true also.

I've read the documentation (all the documents you're gonna recommend) but I still can't figure out how to get this right. Maybe it's my DAO structure, or maybe I'm doing something else wrong. I'd really appreciate if someone could give me some hints on how to get this right.
Tanks in advance.

...
So basically I added the @Transactional to methods that need to write on the DB. At first so I could try this, I just added the @Transactional to the update methods, but it required proxy-target-class set to true also.

I've read the documentation (all the documents you're gonna recommend) but I still can't figure out how to get this right. Maybe it's my DAO structure, or maybe I'm doing something else wrong. I'd really appreciate if someone could give me some hints on how to get this right.
Tanks in advance.

Spring is able to use two proxying mechanisms - jdk- and cglib-based. Feel free to read more about that here. So, if you use jdk proxies, the methods that not present at the interface are not adviced.

Also the statement 'proxy-target-class="true" in the config file ... is not recommended' is incorrect. Both approaches have pros and cons and spring team just recommends to use jdk proxies if possible. If it's not possible or you want to get slight performance boost there is absolutely no objection to use cglib proxies.

Comment

I know the two methods work, what I don't know is why I'm being forced to use CGLIB (due to the errors I get if I don't). I'd like to know what I'm doing that prevents me of working with the JDK proxies if I remove the proxy-target-class setting.

Comment

I know the two methods work, what I don't know is why I'm being forced to use CGLIB (due to the errors I get if I don't). I'd like to know what I'm doing that prevents me of working with the JDK proxies if I remove the proxy-target-class setting.

I already told about that - if you use jdk proxies, the methods that not present at the interface are not adviced. I.e. jdk-proxies are aware only about interface methods, so, tx aspect logic may be applied only at interface method call.