c3p0与dbcp 数据源配置方式

来源:互联网 发布:java注解类 编辑:程序博客网 时间:2024/06/13 19:23

dbcp数据源连接

<bean id="dataSource" class="org.apache.commons.dbcp.BaseDataSource" destory-method="close">
 <property name="driverClassName" value="com.mysql.jdbc.Driver" />
 <property name="url" value="jdbc:mysql:///userlogin_ssh?useUnicode=true&amp;characterEncoding=UTF-8" />
 <property name="username" value="root" />
 <property name="password" value="root" />
 <property name="maxActive" value="100" />
 <property name="maxIdle" value="30" />
 <property name="maxWait" value="500" />

 <!-- defaultAutoCommit属性,值为true时表示每执行完一个数据库操作后会自动进行提交-->
 <property name="defaultAutoCommit" value="true" />
</bean>
<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.MySQL5Dialect</prop>
  <prop key="hibernate.show_sql">false<prop>
 </props>
 </property>
 <property name="mappingResources">
  <list>
  <value>com/test/model/User.hbm.xml</value>
  </list>
 </property>
</bean>

 

在老黎的SSH2整合中
spring 采用注解管理bean
配置c3p0数据源
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destory-method="close">
 <property name="driverClass" value="org.gjt.mm.mysql.Driver"/>
 <property name="jdbcUrl" value="jdbc:mysql:///itcast?useUnicode=true&amp;characterEncoding=UTF-8" />
 <property name="user" value="root" />
 <property name="password" value="root" />
 <property name="initialPoolSize" value="1" />
 <property name="minPoolSize" value="1" />
 <property name="maxPoolSize" value="300" />
 <!-- 最大空闲时间,60表示60秒内无使用,则连接被丢弃,如果为0,表示始终保持连接 Default:0-->
 <property name="maxIdleTime" value="60" />
 <!-- 当连接池中的连接耗尽时,c3p0一次同时获取的连接数 Default:3 -->
 <property name="acquireIncrement" value="5" />
 <!-- 每60秒检查所有连接池中的空闲连接 Default:0-->
 <property name="idleConnectionTestPeriod" value="60" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 <property name="dataSource" ref="dataSource" />
 <property name="mappingResources">
  <list>
  <value>cn/itcast/bean/Person.hbm.xml</value>
  </list>
 </property>
 <property name="hibernateProperties">
  <value>
   hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
   hibernate.hbm2ddl.auto=update
   hibernate.show_sql=false
   hibernate.format_sql=false
  </value>
 </property>
</bean>
<!--配置事务管理器-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
 <property name="sessionFactory" ref="sessionFactory" />
</bean> 
<!--bean到底使用什么方式进行管理,是采用Annotation方式还是基于xml方式来配置事务-->
<tx:annotation-driven transaction-manager="txManager" />

 

cp配置:
<Context path="/mysssh3" docBase="D:/ssh3/mysssh3/WebRoot" reloadable="true" />
 <Resource name="jdbc/sssh3" auth="Container"
 type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
 url="jdbc:mysql://localhost:3306;DataBaseName=sssh3_db"
 username="root" password="root" maxActive="20" maxIdle="10" maxWait="-1"/>

原创粉丝点击