首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

ibati整合spring的事物管理有关问题

2012-02-19 
ibati整合spring的事物管理问题1、public class TestClass extends TestCase {public void testInsertAll()

ibati整合spring的事物管理问题
1、public class TestClass extends TestCase {
  public void testInsertAll(){
  Client client = new Client();
client.insertAll();
  }
}


2、
public class Client {
ApplicationContext factory = new ClassPathXmlApplicationContext(
"applicationContext.xml");

public void insertStudent() {
StudentDao studentDao = (StudentDao) factory.getBean("studentDao");
Student student = new Student();
student.setEndTime("lishipeng");
student.setName("lishipeng");
student.setSex("lishipeng");
student.setStartTime("lishipeng");
studentDao.insertStudent(student);
}

public void insertFather() {
FatherDao fatherDao = (FatherDao) factory.getBean("fatherDao");
Father father = new Father();
father.setEndTime("lishipeng");
father.setName("lishipeng");
father.setSex("lishipeng");
father.setStartTime("lishipeng");
fatherDao.insertFather(father);
}

public void insertAll() {
insertFather();
insertStudent();
}




}

3、<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans  
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  http://www.springframework.org/schema/context  
  http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!--此bean告诉Spring去哪找数据库的配置信息,因为有此Bean才出现下面用${}标记来取变量的语句-->
<bean id="propertyConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>configfile/jdbc.properties</value>
</property>
</bean>

<!--配置一个数据源,根据上面propertyConfig指定的location去找数据库连接的配置信息-->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${jdbc.driver}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>

<!-- Spring声明式事务管理 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="select*" read-only="true" />


</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="allManagerMethod"
expression="execution(public * cn.client.*.*(..))" />
<aop:advisor pointcut-ref="allManagerMethod"
advice-ref="txAdvice" />
</aop:config>

<!--根据dataSource和configLocation创建一个SqlMapClient-->
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>configfile/sql-map-config.xml</value>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>

<!--根据sqlMapClien创建一个SqlMapClient模版类-->
<bean id="sqlMapClientTemplate"
class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>

<!--将上面的模版类织入到我们的DAO对象中-->
 

<bean id="studentDao" class="cn.itcastImpl.StudentDaoSqlMap">
<property name="sqlMapClientTemplate">
<ref bean="sqlMapClientTemplate" />
</property>
</bean>
<bean id="fatherDao" class="cn.itcastImpl.FatherDaoSqlMap">
<property name="sqlMapClientTemplate">
<ref bean="sqlMapClientTemplate" />
</property>
</bean>
</beans>

现在出现的问题是:我有意的将其中一个表中的数据差错,然后后台报错,但是为什么还有一个表插入插入成功呢?应该其中一个表进行回滚呀。。。。。。。。


[解决办法]
顶一下!!!!!

热点排行