Spring事务管理器_注解方式
让hibernate 的实现方法具有事务性,用spring事务管理器进行切面管理。
若不具备事务性,
???? 1)dataSource池 无法管理 连接池,执行一条DB操作,将会打开一个新连接,直到消耗了20个连接后,挂掉,且无法自动回复db 连接。
???? 2)没有事务性的系统,运行的安全性,可想而知了。
下面是一种比较被合理采用的,也是比较灵活的,注解方式 声明 给 spring 事务管理器。
配置文件比较简单,无须扩展,只要在操作DB时,方法声明? @Transactional 即可。
?
1: applicationContext.xml ----------------------------------------
<?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: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 id="dataSource" value="com.mysql.jdbc.Driver"></property>
??? ??? <property name="url" value="jdbc:mysql://localhost:3306/wysosdb?useUnicode=true&characterEncoding=utf-8"></property>
??? ??? <property name="username" value="root"></property>
??? ??? <property name="password" value="softdn"></property>
??? ??? <property name="maxActive" value="100"></property>
??? ??? <property name="maxIdle" value="10"></property>
??? ??? <property name="maxWait" value="500"></property>
??? ??? <property name="defaultAutoCommit" value="false"></property>
??? </bean>
??? <!-- hibernate 的会话工厂 -->
??? <bean id="sessionFactory"
??? ??? value="classpath:hibernate.cfg.xml" />
??? ??? <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
??? ??? <property name="dataSource" ref="dataSource"></property>
??? ??? <property name="hibernateProperties">
??? ??? ??? <props>
??? ??? ??? ??? <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
??? ??? ??? ??? <prop key="hibernate.show_sql">true</prop><!-- 显示SQL,为了方便测试 -->
??? ??? ??? </props>
??? ??? </property>
??? ??? <property name="mappingResources">
??? ??? ??? <list><!-- 映射文件 -->
??? ??? ??? ??? <value>db/table/Member.hbm.xml</value>
??? ??? ??? </list>
??? ??? </property>
??? </bean>
??? ???
??? <!-- 定义事务管理器(声明式的事务)(针对hibernate的事务管理器) -->
??? <bean id="txManager" ref="sessionFactory"></property>
??? </bean>
??? <!-- 采用@Transaction注解的方式使用事务 -->
??? <tx:annotation-driven transaction-manager="txManager"/>??????
???
??? <!-- 把Session工厂注入给hibernateTemplate -->
??? <!-- 解释一下hibernateTemplate:hibernateTemplate提供了很多方便的方法,在执行时自动建立 HibernateCallback 对象,例如:load()、get()、save、delete()等方法。 -->
??? <bean id="hibernateTemplate" scope="singleton">
??? ??? <property name="sessionFactory" ref="sessionFactory"></property>
??? </bean>
??? <bean id="memberService" scope="prototype">
??? ??? <property name="memberDao" ref="memberDao"></property>
??? </bean>
?
</beans>
?
?
2:daoImpl 方法调用
??? /**
??? ?* 根据 VO 查询 List
??? ?*/
??? @Transactional
??? public List<Task> listTaskByExample(Task task, Order order)
??? ??? ??? throws Exception {
??? ??? List<Task> list = null;
??? ??? // -----1,Check session is open or not, and open it
??? ???
??? ??? // -----2,Create criteria for the bean class
??? ??? Criteria cs? = this.getSession().createCriteria(Task.class);
??? ??? cs.add(Example.create(task));
??? ??? if (null != task.getTaskId()){
??? ??? ??? cs.add(Expression.eq("taskId", task.getTaskId()));
??? ??? }
??? ???
??? ??? // -----3,Build the Order
??? ??? if (order != null){
??? ??? ??? cs.addOrder(order);
??? ??? }
??? ???
??? ??? // -----4,Exe
??? ??? list =? cs.list();
??? ???
??? ??? // -----5,finally Close session
??? ???
??? ??? // -----6,Return
??? ??? return list;??? ???
??? }