1.数据源 c3p0
<context:property-placeholder
location="classpath:cn/com/legendapl/persistence/config/database.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="minPoolSize" value="${c3p0.minPoolSize}" />
<property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
<property name="initialPoolSize" value="${c3p0.initialPoolSize}" />
</bean>
2.EntityManagerFactory
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
</bean>
</property>
</bean>
3.事务管理器
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!--
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* *..dao.impl..*(..))" id="dao-pc"/>
<aop:pointcut expression="execution(* *..service.impl..*(..))" id="service-pc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="dao-pc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="service-pc"/>
</aop:config>
-->
<tx:annotation-driven/>
4.异常转换
※要转换的组件上要加入@Repository标注
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
5.classpath*:persistence.xml
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
>
<persistence-unit name="hibernate" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>cn.com.legendapl.persistence.domain.User</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<!--
<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>
-->
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.jdbc.fetch_size" value="18" />
<property name="hibernate.jdbc.batch_size" value="10" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
6.DaoImpl例 (片段)
不要在DaoImpl的方法中使用 emf.createEntityManager();
这样为手动控制事务,事务的配置失效。
@Repository
public class UserDaoImpl implements UserDao {
@Resource(name = "entityManagerFactory")
protected EntityManagerFactory emf;
public void saveUser(User user) {
EntityManager em = EntityManagerFactoryUtils.getTransactionalEntityManager(emf);
em.persist(user);
System.out.println(em.getFlushMode());
}
public User findUserById(Integer id) {
EntityManager em = EntityManagerFactoryUtils.getTransactionalEntityManager(emf);
return em.find(User.class, id);
}
}
7.数据库信息(database.properties)
<pre name="code" class="plain">#====================================================================================
# jdbc infomation
# jdbc.dirverClass : 驱动类名
# jdbc.url : 数据库统一资源描述符
# jdbc.username : 数据库登录用户名
# jdbc.password : 数据库登录口令
#====================================================================================
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/income
jdbc.username=root
jdbc.password=root
#====================================================================================
# c3p0 setting
# c3p0.minPoolSize : 连接池最小保存的连接数量
# c3p0.maxPoolSize : 连接池最大保存的连接数量
# c3p0.initialPoolSize : 连接池被创建时保存的连接数量
# c3p0.maxIdleTime : 连接的最大空闲时间,超时不被使用则丢弃
# c3p0.acquireIncrement : 当连接耗尽时每次申请的连接数量
# c3p0.idleConnectionTestPeriod : 每间隔一定秒数秒检查所有连接池中的空闲连接
# c3p0.acquireRetryAttempts : 定义在从数据库获取新连接失败后重复尝试的次数
#====================================================================================
c3p0.minPoolSize=5
c3p0.maxPoolSize=30
c3p0.initialPoolSize=10
#c3p0.maxIdleTime=60
#c3p0.acquireIncrement=3
#c3p0.idleConnectionTestPeriod=60
#c3p0.acquireRetryAttempts=30
#c3p0.breakAfterAcquireFailure=true
#====================================================================================
# hibernate setting
# hibernate.dialect : 方言
#====================================================================================
hibernate.dialect=org.hibernate.dialect.MySQLDialect