1.Spring
Spring自带的数据源配置
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://127.0.0.1:3306/spring_test"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource1"></property> <property name="configLocations" value="classpath:hibernate.cfg.xml"></property> </bean>
|
2.c3p0
依赖包:
1 2
| c3p0-0.9.2.1.jar mchange-commons-java-0.2.3.4.jar
|
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <bean id="dataSource2" class="com.mchange.v2.c3p0.DriverManagerDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/spring_test"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource2"></property> <property name="configLocations" value="classpath:hibernate.cfg.xml"></property> </bean>
|
3.dbcp
依赖包:
1 2
| commons-dbcp-1.4.jar commons-pool-1.6.jar
|
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <bean id="dataSource3" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://127.0.0.1:3306/spring_test"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource3"></property> <property name="configLocations" value="classpath:hibernate.cfg.xml"></property> </bean>
|
4.druid
依赖包:
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <bean id="dataSource4" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://127.0.0.1:3306/spring_test"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource4"></property> <property name="configLocations" value="classpath:hibernate.cfg.xml"></property> </bean>
|
注: druid提供了web端可视化界面,可以监控查看连接池的详细情况。