其实原理都是一样 就是用一个继承了实现javax.sql.DataSource的DataSource类
在Spring中用bean来组装,管理 来配置其属性 提供给sessionFactory来组装
API上可能属性没有写出来 但是可以看到方法要想真正了解 还是要看源代码中的详细属性
比如在cp30中这个具体的DataSource是
com.mchange.v2.c3p0.ComboPooledDataSource
他的源代码中有以下片段:
{ return dmds.getDriverClass();}
public void setDriverClass(StringdriverClass) throws PropertyVetoException
{
dmds.setDriverClass(driverClass);
// System.err.println("settingdriverClass:"+driverClass);
}
public StringgetJdbcUrl()
{
// System.err.println("gettingjdbcUrl:"+dmds.getJdbcUrl());
return dmds.getJdbcUrl();
}
public void setJdbcUrl(StringjdbcUrl)
{
dmds.setJdbcUrl(jdbcUrl);
this .resetPoolManager( false );
// System.err.println("settingjdbcUrl:"+jdbcUrl+"[dmds@"+C3P0ImplUtils.identityToken(dmds)+"]");
// if(jdbcUrl==null)
// newException("***NULLSETTER***").printStackTrace();
}
public PropertiesgetProperties()
{
// System.err.println("gettingproperties:"+dmds.getProperties());
return dmds.getProperties();
}
public void setProperties(Propertiesproperties)
{
// System.err.println("settingproperties:"+properties);
dmds.setProperties(properties);
this .resetPoolManager( false );
}
// DriverManagerDataSource"virtualproperties"basedonproperties
public StringgetUser()
{ return dmds.getUser();}
public void setUser(Stringuser)
{
dmds.setUser(user);
this .resetPoolManager( false );
}
public StringgetPassword()
{ return dmds.getPassword();}
public void setPassword(Stringpassword)
{
dmds.setPassword(password);
this .resetPoolManager( false );
里面有个property属性的get和set方法
因此在bean中可以对其进行配置
组装到sessionFactory中之后 还可以通过hibernateProperties对Hibernate进行一些配置
一 c3p0
beas-config.xml
<! DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
<!-- =================================================================== -->
<!-- JDBCdataSource -->
<!-- =================================================================== -->
<!-- ***********************************************************************
<beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<propertyname="url">
<value>jdbc:mysql://localhost:3306/shtest</value>
</property>
<propertyname="username">
<value>root</value>
</property>
<propertyname="password">
<value>password</value>
</property>
</bean>
************************************************************************ -->
<!-- =================================================================== -->
<!-- c3p0dataSource -->
<!-- =================================================================== -->
< bean id ="dataSource" class ="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method ="close" >
< property name ="driverClass" >
< value > com.mysql.jdbc.Driver </ value >
</ property >
< property name ="jdbcUrl" >
< value > jdbc:mysql://localhost:3306/shtest </ value >
</ property >
< property name ="properties" >
< props >
< prop key ="c3p0.acquire_increment" > 2 </ prop >
< prop key ="c3p0.idle_test_period" > 200 </ prop >
< prop key ="c3p0.timeout" > 1000 </ prop >
< prop key ="c3p0.max_size" > 100 </ prop >
< prop key ="hibernate.c3p0.max_statements" > 100 </ prop >
< prop key ="hibernate.c3p0.min_size" > 20 </ prop >
< prop key ="user" > root </ prop >
< prop key ="password" > password </ prop >
</ props >
</ property >
</ bean >
<!-- =================================================================== -->
<!-- HibernatesessionFactory -->
<!-- =================================================================== -->
< bean id ="sessionFactory" class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method ="close" >
< property name ="dataSource" >
< ref bean ="dataSource" />
</ property >
< property name ="mappingResources" >
< list >
< value > /com/ergal/hibernate/User.hbm.xml </ value >
</ list >
</ property >
< property name ="hibernateProperties" >
< props >
< prop key ="hibernate.dialect" > org.hibernate.dialect.MySQLDialect </ prop >
< prop key ="hibernate.show_sql" > true </ prop >
< prop key ="hibernate.current_session_context_class" > thread </ prop >
</ props >
</ property >
</ bean >
<!-- =================================================================== -->
<!-- HibernatePOJO -->
<!-- =================================================================== -->
< bean id ="userDao" class ="com.ergal.hibernate.UserDao" >
< property name ="sessionFactory" >
< ref bean ="sessionFactory" />
</ property >
</ bean >
<!-- =================================================================== -->
<!-- transactionManager -->
<!-- =================================================================== -->
< bean id ="transactionManager" class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
< property name ="sessionFactory" >
< ref bean ="sessionFactory" />
</ property >
</ bean >
<!-- =================================================================== -->
<!-- DaoProxy -->
<!-- =================================================================== -->
< bean id ="userDaoProxy" class ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
< property name ="transactionManager" >
< ref bean ="transactionManager" />
</ property >
< property name ="proxyInterfaces" >
< list >
< value > com.ergal.hibernate.IUserDao </ value >
</ list >
</ property >
< property name ="target" >
< ref bean ="userDao" />
</ property >
< property name ="transactionAttributes" >
< props >
< prop key ="insert" > PROPAGATION_REQUIRED </ prop >
</ props >
</ property >
</ bean >
</ beans >
这么一来在修改数据库连接的部分就变的非常简单 修改后也不用修改除sessionFactory的组装外的其他代码
二 proxool
这个可以通过对hibernateProperties的属性配置来设置
beans-config.xml
< property name ="mappingResources" >
< list >
< value > com/meagle/bo/Order.hbm.xml </ value >
< value > com/meagle/bo/OrderLineItem.hbm.xml </ value >
</ list >
</ property >
< property name ="hibernateProperties" >
< props >
< prop key ="hibernate.dialect" > net.sf.hibernate.dialect.MySQLDialect </ prop >
< prop key ="hibernate.show_sql" > true </ prop >
< prop key ="hibernate.cglib.use_reflection_optimizer" > true </ prop >
< prop key ="hibernate.proxool.xml" > C:/....../WebContent/WEB-INF/proxool.xml </ prop >
< prop key ="hibernate.proxool.pool_alias" > spring </ prop >
</ props >
</ property >
</ bean >
然后单独写一个proxool.xml
<!-- theproxoolconfigurationcanbeembeddedwithinyourownapplication's.
Anythingoutsidethe"proxool"tagisignored. -->
< proxool >
< alias > spring </ alias >
< driver-url > jdbc:mysql://localhost:3306/shtest </ driver-url >
< driver-class > com.mysql.jdbc.Driver </ driver-class >
< driver-properties >
< property name ="user" value ="root" />
< property name ="password" value ="password" />
</ driver-properties >
< minimum-connection-count > 2 </ minimum-connection-count >
< maximum-connection-count > 20 </ maximum-connection-count >
< maximum-connection-lifetime > 180000 </ maximum-connection-lifetime > <!-- 5hours -->
< house-keeping-test-sql > values(currentTimeStamp) </ house-keeping-test-sql >
< statistics > 1m,15m,1d </ statistics >
< statistics-log-level > INFO </ statistics-log-level >
< fatal-sql-exception > Connectionisclosed,SQLSTATE=08003,Erroropeningsocket.SQLSTATE=08S01,SQLSTATE=08S01 </ fatal-sql-exception >
< fatal-sql-exception-wrapper-class > org.logicalcobwebs.proxool.FatalRuntimeException </ fatal-sql-exception-wrapper-class >
< verbose > false </ verbose >
</ proxool >