SSH中hibernate的事务配置

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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
  <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName">
      <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
    </property>
    <property name="url">
      <value>jdbc:microsoft:sqlserver://localhost:1433</value>
    </property>
    <property name="username">
      <value>sa</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.SQLServerDialect
        </prop>
        <prop key="hibernate.show_sql">true</prop>
      </props>
    </property>
    <property name="mappingResources">
      <list>
        <value>beans/Users.hbm.xml</value>
      </list>
    </property>
  </bean>
  <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
      <ref local="sessionFactory"/>
    </property>
  </bean>
  <bean id="UsersDAO" class="beans.UsersDAO">
    <property name="sessionFactory">
      <ref bean="sessionFactory" />
    </property>
  </bean>
  <bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager">
      <ref local="transactionManager"/>
    </property>
    <!-- 指定直接对类进行代理,将属性proxyTargetClass指定为true(默认是false) -->
    <property name="proxyTargetClass" value="true"/>
    <property name="target">
      <ref local="UsersDAO"/>
    </property>
    <property name="transactionAttributes">
      <props>
        <!-- key表示匹配DAO中的方法名,例如:save*表示所有前缀为save的方法 -->
        <prop key="save*">PROPAGATION_REQUIRED</prop>
        <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
        <prop key="delete*">PROPAGATION_REQUIRED</prop>
        <prop key="update*">PROPAGATION_REQUIRED</prop>
      </props>
    </property>
  </bean>
</beans>
<!--
PROPAGATION_REQUIRED 支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。 
PROPAGATION_SUPPORTS 支持当前事务,如果当前没有事务,就以非事务方式执行。 
PROPAGATION_MANDATORY 支持当前事务,如果当前没有事务,就抛出异常。 
PROPAGATION_REQUIRES_NEW 新建事务,如果当前存在事务,把当前事务挂起。 
PROPAGATION_NOT_SUPPORTED 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。 
PROPAGATION_NEVER 以非事务方式执行,如果当前存在事务,则抛出异常。 
PROPAGATION_NESTED 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。 
-->


UsersDAO

package beans;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.marker.spring.MyHibernateDao;
import org.springframework.context.ApplicationContext;

public class UsersDAO extends MyHibernateDao implements UsersInterface
{
    private static final Log log = LogFactory.getLog(UsersDAO.class);
    public static final String NAME = "name";
    public static final String PWD = "pwd";
    public void save(Users transientInstance)
    {
        log.debug("saving Users instance");
        try
        {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        }
        catch (RuntimeException re)
        {
            log.error("save failed", re);
            throw re;
        }
    }
    public void delete(Users persistentInstance)
    {
        log.debug("deleting Users instance");
        try
        {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        }
        catch (RuntimeException re)
        {
            log.error("delete failed", re);
            throw re;
        }
    }
    public Users findById(java.lang.Integer id)
    {
        log.debug("getting Users instance with id: " + id);
        try
        {
            Users instance = (Users) getHibernateTemplate().get("beans.Users",
                    id);
            return instance;
        }
        catch (RuntimeException re)
        {
            log.error("get failed", re);
            throw re;
        }
    }
    public void update(Users user)
    {
        try
        {
            getHibernateTemplate().update(user);
            log.debug("update successful");
        }
        catch (RuntimeException re)
        {
            log.error("update failed",re);
        }
    }
}

你可能感兴趣的:(Hibernate)