现在的位置: 首页 > 综合 > 正文

JTA技术实现

2018年02月12日 ⁄ 综合 ⁄ 共 4666字 ⁄ 字号 评论关闭

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="net.sourceforge.jtds.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/test">
</property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
<property name="maxActive">
<value>200</value>
</property>
<property name="maxIdle">
<value>70</value>
</property>
<property name="minIdle">
<value>60</value>
</property>
<property name="maxWait">
<value>2000</value>
</property>
<property name="initialSize">
<value>60</value>
</property>
<property name="removeAbandoned">
<value>true</value>
</property>
<property name="removeAbandonedTimeout">
<value>60</value>
</property>
<property name="logAbandoned">
<value>true</value>
</property>
</bean>
<bean id="newsDataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="net.sourceforge.jtds.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/test2">
</property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
<property name="maxActive">
<value>200</value>
</property>
<property name="maxIdle">
<value>70</value>
</property>
<property name="minIdle">
<value>60</value>
</property>
<property name="maxWait">
<value>2000</value>
</property>
<property name="initialSize">
<value>60</value>
</property>
<property name="removeAbandoned">
<value>true</value>
</property>
<property name="removeAbandonedTimeout">
<value>60</value>
</property>
<property name="logAbandoned">
<value>true</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>demo/jpa/pojo/Student.hbm.xml</value>
</list>
</property>
</bean>
<bean id="newsSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="newsDataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>demo/jpa/pojo/Teacher.hbm.xml</value>
</list>
</property>
</bean>
<bean id="jotm"
class="org.springframework.transaction.jta.JotmFactoryBean" />
<bean id="myTxManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="jotm" />
</bean>
<!-- 配置事务特性-->
<tx:advice id="txAdvice" transaction-manager="myTxManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置那些类的方法进行事务管理-->
<aop:config>
<aop:pointcut id="allManagerMethod"
expression="execution (* demo.jpa.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="allManagerMethod" />
</aop:config>

<bean id="studentDAO" class="demo.jpa.dao.impl.StudentDAOImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>

<bean id="teacherDAO" class="demo.jpa.dao.impl.TeacherDAOImpl">
<property name="sessionFactory">
<ref local="newsSessionFactory" />
</property>
</bean>

<bean id="studentService" class="demo.jpa.service.impl.StudentServiceImpl">
<property name="studentDAO">
<ref local="studentDAO"/>
</property>
<property name="teacherDAO">
<ref local="teacherDAO"/>
</property>
</bean>

</beans>

public class TestJPA
{
@Test
public void saveCustomerAndOrder()
{
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

IStudentService studentService=(IStudentService)ctx.getBean("studentService");

Student student = new Student();
student.setSname("张三");

Teacher teacher = new Teacher();
teacher.setTname("李四");

studentService.saveStudent(student, teacher);
}
}

抱歉!评论已关闭.