Jakarta 的公共连接池实现 - BasicDataSource
如果你的手头没有连接池的本地( native )实现,你可以使用 Jakarta 提供的公共连接池实现 [org.apache.commons.dbcp.BasicDataSource] ,它可以和 DataSource 管理器 " 合作 " 的很好。另 外, Struts 还在它的 util 包里包含了一个 GenericDataSource 类,这也是一个连接池实现。但是这只是一个非常简单的实现方案,不推 荐使用,因为它可能在 Struts 的以后版本中被 BasicDataSource 或其它的数据源实现替换掉。
taken from :http://fqmly520.iteye.com/blog/154738
以下配置 org.apache.commons.dbcp.BasicDataSource :
< bean id = "dataSource"
class = "org.apache.commons.dbcp.BasicDataSource" destroy-method = "close" >
< property name = "driverClassName" >
< value > ${database.driver} </ value >
</ property >
< property name = "url" >
< value > ${database.url } </ value >
</ property >
< property name = "username" >
< value > ${database.username } </ value >
</ property >
< property name = "password" >
< value > ${database.password} </ value >
</ property >
</ bean >
利用 PropertyPlaceholderConfigurer 占位符,则需要配置以下文件 :
< bean id = "propertyConfigurer"
class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
< property name = "locations" >
< list >
< value > classpath:jdbc.properties </ value >
</ list >
</property >
</bean >
jdbc.properties 文件内容 :
#devlopemnt
database.url= jdbc : oracle:thin:@192.168.1.113:1521:orcl
database.driver= oracle.jdbc .driver.OracleDriver
database.username= user
database.password= pass
spring 要整合 hibernate ,需要用到一个关键类 LocalSessionFactoryBean. 配置如下 :
< 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.OracleDialect
</ prop >
< prop key = "hibernate.show_sql" > true </ prop >
< prop key = "hibernate.format_sql" > true </ prop >
< prop key = "hibernate.jdbc.batch_size" > 50 </ prop >
</ props >
</ property >
< property name = "mappingLocations" >
< list > < value > classpath *:com /oss /db /model/**/*.hbm .xml </ value >
</ list >
</ property >
</ bean >
如果一个方法中既用了 HibernateTemplate ,又用了 JdbcTemplate ,应该怎么配单实例的 db 事务呢(多例免谈)用 DataSouceTransactionManager 是不行的,而用 HibernateTransactionManager 就可以保证
原因的话看下它们源代码,会发现 HibernateTransactionManager 中的处理可以保证 SessionFactoryUtil 和 datasourceutil 都能在一个事务里取到同一个连接
Taken from: http://bjyzxxds.iteye.com/blog/427309
< tx:advice id = "txAdvice" transaction-manager = "hibernateTransactionManager" >
< tx:attributes >
< tx:method name = "update*" propagation = "REQUIRED" rollback-for = "java.lang.Exception" />
< tx:method name = "save*" propagation = "REQUIRED" rollback-for = "java.lang.Exception" />
< tx:method name = "auto*" propagation = "REQUIRED" rollback-for = "java.lang.Exception" />
< tx:method name = "insert*" propagation = "REQUIRED" rollback-for = "java.lang.Exception" />
< tx:method name = "execute*" propagation = "REQUIRED" rollback-for = "java.lang.Exception" />
< tx:method name = "add*" propagation = "REQUIRED" rollback-for = "java.lang.Exception" />
< tx:method name = "create*" propagation = "REQUIRED" rollback-for = "java.lang.Exception" />
< tx:method name = "delete*" propagation = "REQUIRED" rollback-for = "java.lang.Exception" />
< tx:method name = "modify*" propagation = "REQUIRED" rollback-for = "java.lang.Exception" />
< tx:method name = "fetch*" propagation = "REQUIRED" rollback-for = "java.lang.Exception" />
< tx:method name = "*Business" propagation = "REQUIRED" rollback-for = "java.lang.Exception" />
< tx:method name = "find*" propagation = "REQUIRED" read-only = "true" />
< tx:method name = "get*" propagation = "REQUIRED" read-only = "true" />
< tx:method name = "check*" propagation = "REQUIRED" read-only = "true" />
</ tx:attributes >
</ tx:advice >
< aop:config proxy-target-class = "true" >
< aop:pointcut id = " interceptorPointCuts" expression = "execution(* com.oss.service..*.*(..))" />
< aop:advisor advice-ref = "txAdvice" pointcut-ref = " interceptorPointCuts" />
</ aop:config >
< bean id = "hibernateTransactionManager"
class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
< property name = "sessionFactory" >
< ref local = "sessionFactory" />
</ property >
</ bean >
// 以上配置事务传播特性并把 sessionFactory 纳入管理
< bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate" >
< property name = "dataSource" ref = "dataSource" />
</ bean >