Back Forum Reply New

One Transaction for each DAO

Hello!, I have a problem with my Transaction. The problem is that Spring is generating one Transaction per Dao and I can't rollback the changes in an previous insert or update when an error ocurred.

My definition is:HTML Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;!DOCTYPE beans PUBLIC quot;-//SPRING//DTD BEAN//ENquot; quot;dtd/spring-beans.dtdquot;gt;

lt;beansgt;
   lt;import resource=quot;dataSourceFactoryBeans.xmlquot;gt;lt;/importgt;
   lt;bean id=quot;dataSourcequot; class=quot;javax.sql.DataSourcequot; factory-bean=quot;portalDataSourceFactoryquot; factory-method=quot;getDataSourcequot;gt;   lt;/beangt;
   lt;bean id=quot;hibernateTransactionManagerquot; class=quot;org..orm.hibernate3.HibernateTransactionManagerquot;gt;       lt;property name=quot;dataSourcequot;gt;lt;ref bean=quot;dataSourcequot;/gt;       lt;/propertygt;       lt;property name=quot;sessionFactoryquot;gt;lt;ref local=quot;portalSessionFactoryquot;/gt;lt;/propertygt;   lt;/beangt;
   lt;bean id=quot;abstractSessionFactoryquot; abstract=quot;truequot; class=quot;org..orm.hibernate3.LocalSessionFactoryBeanquot;gt;       lt;property name=quot;dataSourcequot; ref=quot;dataSourcequot;/gt;       lt;property name=quot;mappingResourcesquot;gt;          lt;listgt;    lt;valuegt;com/tonline/albura/osiris/business/hibernate/Accion.hbm.xmllt;/valuegt;    lt;valuegt;com/tonline/albura/osiris/business/hibernate/CoberturaRed.hbm.xmllt;/valuegt;         lt;/listgt;       lt;/propertygt;   lt;/beangt;
   lt;bean id=quot;portalSessionFactoryquot; class=quot;org..orm.hibernate3.LocalSessionFactoryBeanquot; parent=quot;abstractSessionFactoryquot;gt;       lt;property name=quot;hibernatePropertiesquot;gt;         lt;propsgt;  lt;prop key=quot;hibernate.dialectquot;gt;org.hibernate.dialect.OracleDialectlt;/propgt;  lt;prop key=quot;hibernate.jdbc.batch_sizequot;gt;0lt;/propgt;  lt;prop key=quot;hibernate.jdbc.use_streams_for_binaryquot;gt;truelt;/propgt;  lt;prop key=quot;hibernate.connection.SetBigStringTryClobquot;gt;falselt;/propgt;  lt;prop key=quot;hibernate.cache.use_query_cachequot;gt;falselt;/propgt;  lt;prop key=quot;hibernate.cache.provider_classquot;gt;org.hibernate.cache.HashtableCacheProviderlt;/propgt;  lt;prop key=quot;hibernate.show_sqlquot;gt;truelt;/propgt;         lt;/propsgt;       lt;/propertygt;   lt;/beangt;
   lt;bean id=quot;hibernateDaoquot; abstract=quot;truequot; class=quot;com.ya.telco.dataAccessTools.HibernateDAOquot;gt;       lt;property name=quot;dataSourcequot;gt;lt;ref bean=quot;dataSourcequot;gt;lt;/refgt;       lt;/propertygt;       lt;property name=quot;componentquot;gt;lt;ref bean=quot;componentquot;gt;lt;/refgt;       lt;/propertygt;       lt;property name=quot;sessionFactoryquot;gt;lt;ref local=quot;portalSessionFactoryquot;/gt;lt;/propertygt;   lt;/beangt;
   lt;bean name=quot;proxyHibernatequot; abstract=quot;truequot;       class=quot;org..transaction.interceptor.TransactionProxyFactoryBeanquot;gt;       lt;property name=quot;transactionManagerquot;gt;lt;ref bean=quot;hibernateTransactionManagerquot;/gt;       lt;/propertygt;       lt;property name=quot;transactionAttributesquot;gt;lt;propsgt;    lt;prop key=quot;*quot;gtROPAGATION_REQUIREDlt;/propgt;lt;/propsgt;       lt;/propertygt;   lt;/beangt;
   lt;bean name=quot;coberturaRedHibquot; class=quot;com.tonline.albura.osiris.business.hibernate.CoberturaRedquot; singleton=quot;falsequot;gt;       lt;property name=quot;coberturaRedDAOHibquot;gt;lt;ref bean=quot;coberturaRedDAOHibquot;gt;lt;/refgt;       lt;/propertygt;   lt;/beangt;   lt;bean name=quot;coberturaRedDAOHibquot; class=quot;org..transaction.interceptor.TransactionProxyFactoryBeanquot;       parent=quot;proxyHibernatequot; lazy-init=quot;truequot;gt;       lt;property name=quot;targetquot;gt;lt;bean class=quot;com.tonline.albura.osiris.dao.hibernate.CoberturaRedDAOImplquot; parent=quot;hibernateDaoquot;/gt;       lt;/propertygt;   lt;/beangt;
   lt;bean name=quot;componentquot; class=quot;com.app.second.osiris.OsirisComponentquot;gt;lt;/beangt;lt;/beansgt;
Can I have the same Transaction for all the DAO operations??. I need to Rollback a previous DAO operation when the actual DAO operation crash a throws an Exception.

Thanks

pd: I speak English a little (I´m Spanish  )

If you are proxying your Dao with TransactionProxyFactoryBean then all transactions will be at the Dao level.  What your would need to do is proxy something higher up the stack.  It is typical to apply transactions at the service level.

Current

Code:
Do something with dao.
[Proxy]   [TransactionProxyFactoryBean]       [New Transaction][Do stuff in Dao]       [End Transaction]   [End]
[End]

Do something else with dao.
[Proxy]   [TransactionProxyFactoryBean]       [New Transaction][Do stuff in Dao]       [End Transaction]   [End]
[End]
What you want is.

Code:
Do something with service.
[Proxy]   [TransactionProxyFactoryBean]       [New Transaction][Do stuff in Dao][Do stuff in Dao]       [End Transaction]   [End]
[End]Thanks, I just create beans at service level with the TransactionProxy asociations and this run corretly.

Glad it solved your problem!  It usually makes sense to provide transactions at the service layer anyway.

But the Service Layer is still very usefull: it is a gateway to the internals of your system and this makes it an excelent place to coordinate (crosscutting) concerns like transactions and security.

?p=17
¥
Back Forum Reply New