View Full Version : Non-transactional Method not writing to database
drinnovations
Sep 6th, 2006, 10:52 AM
Hi--
How can I declare a method --that persists to a file --Non-transactional?
The idea is that anything happens in this method, this method doesn't affect the normal processing of the ongoing transaction.
Or does this, needs to be thought from a different perspective?
Thanks!
Andreas Senft
Sep 6th, 2006, 11:05 AM
Well, if the method has nothing to do with transactions, then you do not need to declare anything about it.
Regards,
Andreas
drinnovations
Sep 6th, 2006, 11:34 AM
>>Well, if the method has nothing to do with transactions, then you do not need to declare anything about it.
Precisely, that's what I thought. However, this runs in a J2EE Env.
If the EJB method has transaction, when this Spring Managed POJO gets called, transaction will propogate to it.
I just want that Transaction is NOT propogated to it.
If this was persisting to a database, you could use DataSourceTransactionManager. But I want to declare it so that independent of its persistence nature(currently Spring Managed POJO does persistence to file), it never allows Transaction propogation to itself. Since it runs in J2EE env. using JtaTransactionManager makes sense?
Again, does this need to be thought from a different perspective?
Andreas Senft
Sep 7th, 2006, 12:49 AM
If your method does not participate in any transaction (writing into a file does not count) then it would not harm that the transactional context of an invoking method would get passed in.
The only way to not somehow prevent "pollution" of the passed in transactional context would be to declare a transactional attribute of "REQUIRES_NEW" which should suspend an existing transaction and create a new one. But I don't see that as necessary. Disabling a transactional context and resume it afterwards is IMHO not possible.
As of the choice of the transaction manager: that depends. If you work with one database only and have no other transactional resources (as JMS for example) you actually do not need JTA (though it would not be harmful to use).
Regards,
Andreas
Costin Leau
Sep 7th, 2006, 01:27 AM
The only way you can avoid the transaction is by moving the code out of the transactional context. Basically if the method will not execute then the whole call chain will stop executing and this will force the tx to commit or rollback - the code that you are running since it's in the chain does affect the transaction outcome.
However, you can use propagation NOT_SUPPORTED (or NEVER if you want an exception to be thrown). Still, to increase performance I would definitely recommend moving the I/O operations out of the call since they can be slow and will affect the transaction execution time.
By committing the tx first you make sure that indeed the I/O operation has to be executed (the rollback signifies that something went wrong) and you don't have to worry about keeping resources (the database) occupied.
drinnovations
Sep 7th, 2006, 08:47 AM
The method in my service POJO gets called from several points in the app similar to log.
Regarding:
>>you can use propagation NOT_SUPPORTED (or NEVER if you want an exception to be thrown)
This is what I want to use. Would something like this work? There is no data source defined however?
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransaction Manager"/>
<bean id="serviceTarget"
class="someServiceclass"/>
<bean id="serviceBean" class="org.springframework.transaction.interceptor.Transa ctionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="target">
<ref bean="serviceTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="methodname in service">PROPAGATION_NOT_SUPPORTED</prop>
</props>
</property>
</bean>
Using PROPAGATION_NOT_SUPPORTED is better or REQUIRES_NEW.
In both cases, if this works, the ongoing transaction should not be affected-- will be resumed after the POJO service method finishes.
drinnovations
Sep 8th, 2006, 09:16 AM
Any feedback is welcome. Thanks!
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.