Spring+Hibernate 配置和使用c3p0和proxool连接

系统 1788 0

其实原理都是一样 就是用一个继承了实现javax.sql.DataSource的DataSource类

在Spring中用bean来组装,管理 来配置其属性 提供给sessionFactory来组装

API上可能属性没有写出来 但是可以看到方法要想真正了解 还是要看源代码中的详细属性

比如在cp30中这个具体的DataSource是

com.mchange.v2.c3p0.ComboPooledDataSource
他的源代码中有以下片段:

.............
public StringgetDriverClass()
... { 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

<? xmlversion="1.0"encoding="UTF-8" ?>
<! 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

< bean id ="mySessionFactory" class ="org.springframework.orm.hibernate.LocalSessionFactoryBean" >
< 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

<? xmlversion="1.0"encoding="UTF-8" ?>
<!-- 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 >

Spring+Hibernate 配置和使用c3p0和proxool连接池


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论