1:C3P0数据源的配置、sessionFactory托管给SPRING的配置。

由于C3P0使用比较广泛,下面介绍C3P0在spring和hibernate3配置中的一些常用配置项,首先先把配置文件贴出来先,配置的文件名为dbContext.xml



    
    

    
    

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        
        
        

    

    
    
        

        
        
            
                cn.bb.entity
            
        

        
            
                org.hibernate.dialect.PostgreSQLDialect
                 true
                update
            
        
    

    
    
        

    


这是描述是扫描Entity包的类,也就是我们所说的domain类,orm框架中户数据对于的类


       

           

                cn.bb.entity

           

       

        

这里描述的是采用packagesToScan参数扫描entity,生成数据表。


2:DAO的配置

为了避免框架的侵入,spring不推荐使用hibernateTemplate作为数据访问的入库,而是使用hibernate框架自身的SessionFactory,该SessionFactory在上面dbContext.xml已经配置对应的类是:org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean(具体怎么注入有待研究)。这样在DAO的java代码中只有hibernate框架的代码,而没有spring框架的代码,这样就避免了框架的侵入,职责的单一原则。


package cn.bb.dao;

import cn.bb.entity.Good;
import org.hibernate.SessionFactory;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: John
 * Date: 14-7-1
 * Time: 上午12:52
 * To change this template use File | Settings | File Templates.
 */
public class GoodDao {
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public List list() {
        return this.sessionFactory.getCurrentSession()
                .createCriteria(Good.class)
                .list();
    }

    public void save(Good good) {
        this.sessionFactory.getCurrentSession().save(good);
    }

    public void update(Good good) {
        this.sessionFactory.getCurrentSession().update(good);
    }



}


上面代码中可以发现数据库操作获取hibernatesession的操作都是采用this.sessionFactory.getCurrentSession(),这是Spring官方推荐的,可以提高性能和控制事务。

3:事务控制

采用以上DAO的编码方式必须引入事务控制,因为this.sessionFactory.getCurrentSession()是在当前绑定事务中获取session,当事务不存在时就会保存,如:

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here


采用aop配置事务方式为:applicationContext.xml





    
    

    
        
    
    
        
    

    
        
    

    
        
        
    

    
        
            
            
            
        
    


这样就对GoodService进行了事务控制

GoodService的代码如下:

package cn.bb.service;

import cn.bb.dao.GoodDao;
import cn.bb.entity.Good;

import java.util.List;


public class GoodService {

    private GoodDao goodDao;

    public void setGoodDao(GoodDao goodDao) {
        this.goodDao = goodDao;
    }

    public void increasePriceOfAllGood() {


    }

    public List getAllGood() {
        return goodDao.list();
    }

    public void saveGood(Good good) {
        goodDao.save(good);
    }

    public void saveGoodBatch(List goodList) throws Exception{
        for(Good good : goodList) {
            saveGood(good);
//            goodDao.save(good); //都行
        }
//        throw new Exception("sss"); //不会回滚
        throw new RuntimeException("aa"); //会回滚
    }

}


在测试用,发现在saveGoodBatch抛出 new Exception 不会产生回滚,抛出 RuntimeException时会发生回滚。

4:注意的东西

事务应该控制到service层,在dao和service中不应该捕获异常,也就是出现try..catch..语句,所有异常都在web层进行处理,这样可以让service层的事务进行回滚,从而避免了数据的不一致。

5:pom.xml



  4.0.0
  SpringTransaction
  SpringTransaction
  war
  1.0-SNAPSHOT
  SpringTransaction Maven Webapp
  http://maven.apache.org
  
    
      junit
      junit
      3.8.1
      test
    

      
          jstl
          jstl
          1.2
      


      
      
          javax.servlet
          servlet-api
          2.5
      

      
      
          postgresql
          postgresql
          9.1-901.jdbc4
      
      
      
          c3p0
          c3p0
          0.9.0
      


      
      
          org.slf4j
          slf4j-nop
          1.5.2
      
      
          org.hibernate
          hibernate-core
          3.3.2.GA
      
      
          org.hibernate
          hibernate-annotations
          3.4.0.GA
      
      
          org.hibernate
          hibernate-commons-annotations
          3.3.0.ga
      
      
          org.hibernate
          hibernate-entitymanager
          3.4.0.GA
      

      
      
          dom4j
          dom4j
          1.6.1
      

      
          commons-logging
          commons-logging
          1.1.1
      

      
          commons-collections
          commons-collections
          3.2.1
      

      
          antlr
          antlr
          2.7.7
      

      
          javassist
          javassist
          3.12.1.GA
      

      

      
      
          org.springframework
          spring-test
          3.1.2.RELEASE
      

      
          org.springframework
          spring-core
          3.1.2.RELEASE
      

      
          org.springframework
          spring-context
          3.1.2.RELEASE
      

      
          org.springframework
          spring-web
          3.1.2.RELEASE
      

      
          org.springframework
          spring-webmvc
          3.1.2.RELEASE
      

      
          org.springframework
          spring-orm
          3.1.2.RELEASE
      

      
          org.springframework.security
          spring-security-web
          3.1.6.RELEASE
      
      
          org.springframework.security
          spring-security-config
          3.1.6.RELEASE
      

      
      
          org.springframework
          spring-context-support
          3.1.2.RELEASE
      

      
      
          org.springframework
          spring-tx
          3.1.2.RELEASE
      

      
      
          org.quartz-scheduler
          quartz
          1.8.6
      

      
      
          commons-net
          commons-net
          3.3
      

      
      
          log4j
          log4j
          1.2.16
      

      
      
          org.apache.velocity
          velocity
          1.5
      
      
      
          commons-httpclient
          commons-httpclient
          3.1
      

        
      
          
          
          
      
      
          org.aspectj
          aspectjrt
          1.6.12
      
      
          org.aspectj
          aspectjweaver
          1.6.12
      

  
  
    SpringTransaction