请问个spring里的事务的有关问题
请教个spring里的事务的问题
我用的声明式事务, 请教下,如果我想在一个service方法里 同时调用一个 HibernateDaoSupport实现的dao和一个JdbcDaoSupport实现的dao,能用声明式事务保证这2个dao的事务吗?
只要Hibernate和JDBC在spring配置里面使用同一个数据库连接池,就可以保证在同一个事务里面。(甚至在同一个数据库连接里面)
我用的声明式事务, 请教下,如果我想在一个service方法里 同时调用一个 HibernateDaoSupport实现的dao和一个JdbcDaoSupport实现的dao,能用声明式事务保证这2个dao的事务吗?
1 楼
robbin
2006-10-13
jaesonchen 写道
我用的声明式事务, 请教下,如果我想在一个service方法里 同时调用一个 HibernateDaoSupport实现的dao和一个JdbcDaoSupport实现的dao,能用声明式事务保证这2个dao的事务吗?
只要Hibernate和JDBC在spring配置里面使用同一个数据库连接池,就可以保证在同一个事务里面。(甚至在同一个数据库连接里面)
2 楼
galaxystar
2006-10-13
用同一个datasource的,可以!
3 楼
ddandyy
2006-10-13
如果这两个dao连的是不同的数据库 就要用分布式事务了
4 楼
jaesonchen
2006-10-13
那我在 application.xml里配置事务的时候应该怎么配?
这是我现在的配置:
datasource配置:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>${JDBC_AIOMNI}</value></property>
</bean>
sessionFactory配置:
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="mappingResources">
<list>
...
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
事务管理器配置:(这里是不是应该指向dataSource???)
<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="baseTrasactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
dao的配置:
<bean id="userRoleDao" class="com.asiainfo.sysmanage.dao.impl.UserRoleDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="groupRoleMapJDBCDao" class="com.asiainfo.sysmanage.dao.impl.GroupRoleMapJDBCDaoImpl">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
===============================
然后我想在roleAdminService里保证 userRoleHibernateDao和groupRoleMapJDBCDao使用同一个事物,我下面的配置可以吗?
<bean id="roleAdminService" parent="baseTrasactionProxy">
<property name="target">
<bean class="com.asiainfo.sysmanage.service.impl.RoleAdminServiceImpl">
<property name="userRoleDao">
<ref bean="userRoleHibernateDao"/>
</property>
<property name="groupRoleMapDao">
<ref bean="groupRoleMapJDBCDao"/>
</property>
</bean>
</property>
</bean>
这是我现在的配置:
datasource配置:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>${JDBC_AIOMNI}</value></property>
</bean>
sessionFactory配置:
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="mappingResources">
<list>
...
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
事务管理器配置:(这里是不是应该指向dataSource???)
<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="baseTrasactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
dao的配置:
<bean id="userRoleDao" class="com.asiainfo.sysmanage.dao.impl.UserRoleDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="groupRoleMapJDBCDao" class="com.asiainfo.sysmanage.dao.impl.GroupRoleMapJDBCDaoImpl">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
===============================
然后我想在roleAdminService里保证 userRoleHibernateDao和groupRoleMapJDBCDao使用同一个事物,我下面的配置可以吗?
<bean id="roleAdminService" parent="baseTrasactionProxy">
<property name="target">
<bean class="com.asiainfo.sysmanage.service.impl.RoleAdminServiceImpl">
<property name="userRoleDao">
<ref bean="userRoleHibernateDao"/>
</property>
<property name="groupRoleMapDao">
<ref bean="groupRoleMapJDBCDao"/>
</property>
</bean>
</property>
</bean>
5 楼
galaxystar
2006-10-13
建议用编程事务!