PDA

View Full Version : TransactionProxyFactoryBean


Martin Kersten
Aug 19th, 2004, 05:14 AM
Using the TransactionProxyFactoryBean to advise transactional behaviour, i am missing an option to exclude a method from being advised.

I guess this is a general problem to all proxy factory beans. (or at least, I do not know, how to exclude a method).

Any thoughts on that?

Rod Johnson
Aug 19th, 2004, 06:02 AM
TFP will automatically advise only methods with transaction attributes, through constructing a Pointcut.

The mechanism for controlling which methods are advised in general is a Pointcut. If you add a MethodInterceptor or other Advice implementation without wrapping it in an Advisor, it will affect all methods on the proxied interface(s) or class; if you wrap it in an Advisor you can specify a Pointcut.

Colin Sampaleanu
Aug 19th, 2004, 11:23 AM
Martin may be asking if you can exclude a specific method that would otherwise be included by a more general pointcut regex. Is that the case Martin?

doubleoevan
Aug 30th, 2006, 02:03 AM
Hey there
I am having the hardest time figuring out why my spring/hibernate/struts app wont work
Im using
org.springframework.orm.hibernate3.support.OpenSes sionInViewFilter
as well as
org.springframework.orm.hibernate3.support
Everything is working fine for retrieving and saving but my update methods fail silently
Im not sure but I think it may have something to do with TransactionProxyFactoryBean but Im not sure
Does anyone have any ideas?
Our project development is stuck at this point and we could really use some help and would greatly appreciate it
Thanks in advance
-Evan
doubleoevan@gmail.com

doubleoevan
Aug 31st, 2006, 02:01 AM
Wow Im really scratching my head about this one
When I call getHibernateTemplate().saveOrUpdate to save a new object it works beautifully
But when I call the method to update an object retrieved from the database it fails silently
I with the debugger and found that my updated object appears to have all of the values that Ive set in a class internally called $$EnhancerByCGLIB
But the fields on the main class are null or default so none of my updates get persisted
Is this a lazyinit problem?
Im using the openSessionInViewFilter, does that not handle updates?
If anyone has some ideas I would be eternally grateful
-Evan
P.S.
Heres the code if that helps

protected void saveOrUpdateObject(Object model) throws DataRetrievalException {
try {
setFlushMode(FlushMode.AUTO);
getHibernateTemplate().saveOrUpdate(model);
} catch (Exception e) {
throw new DataRetrievalException("problem saving model " + model, e);
}
}

Costin Leau
Aug 31st, 2006, 05:31 AM
Take a look at the samples provided inside the Spring distribution (petclinic for example) and the ones from the Hibernate distribution (for a pure Hibernate example).
Btw, normally you should not care/set the flush mode manually since that is already taken care of if you are using transactions.

doubleoevan
Sep 3rd, 2006, 12:32 AM
Hey there
I checked out the example and got the TransactionProxyFactoryBean wired up but it didnt work
Maybe it has something to do with my OpenSessionInViewFilter
I read this documentation and tried this:
http://www.springframework.org/docs/api/org/springframework/orm/hibernate/support/OpenSessionInViewFilter.html
Which worked
public class BaseDAOImpl extends HibernateDaoSupport {

protected void saveOrUpdateObject(Object model) throws DataRetrievalException {
try {
setFlushMode(FlushMode.ALWAYS);
getHibernateTemplate().saveOrUpdate(model);
} catch (Exception e) {
throw new DataRetrievalException("problem saving model " + model, e);
}
}

private void setFlushMode(FlushMode mode) {
Session session = getHibernateTemplate().getSessionFactory().getCurr entSession();
session.setFlushMode(mode);
}
}

It feels weird to me though
Should I be setting my flushmode in the applicationContext-hibernate.xml?
If Im doing this the wrong way I would love some advice on how to do it right
Thanks for your help

doubleoevan
Sep 8th, 2006, 01:02 AM
I am really at my wits end here
Thought I had a solution but I guess its back to the drawing board :)
So I am using OpenSessionInViewFilter and Ive found that I can retrieve but when I try to save or update I get the following error:

org.springframework.dao.InvalidDataAccessApiUsageE xception: Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition

I tried blindly to set the FlushMode in the code but that only worked for saves (with FlushMode.AUTO) or updates with (FlushMode.ALWAYS) but never for both when I called getHibernateTemplate().saveOrUpdate

Setting FlushMode.ALWAYS seems wrong anyways
So after looking into the forum a bit more I found that the solution is to use Spring's AOP autoproxying feature to set a transaction advisor

So far I have not been able to do this successfully
If anyone could take a look at the files Ive attached and give me some advise I could really use the support
Thanks for your help
-doubleoevan

doubleoevan
Sep 8th, 2006, 12:52 PM
To see the attachments you have to change the .doc extension to .xml
Sorry about that
I was in a big hurry :)
Thanks for checking it out
-E