前几篇关于Java连接池的介绍都是基于Java应用的,而我们常用的场景是与Spring和ORM框架结合,下面就利用实例学习一下这方面的配置。
1.下载相关内容:
c3p0下载地址:http://sourceforge.net/projects/c3p0/ Proxool下载地址:http://proxool.sourceforge.net/ Druid下载地址:http://code.alibabatech.com/mvn/releases/com/alibaba/druid/ Spring下载地址:http://www.springsource.org/download/community Hibernate下载地址:http://www.hibernate.org/downloads.html ibatis(mybatis)下载地址:http://code.google.com/p/mybatis/
这里我们下载并采用的版本是:c3p0-0.9.2.1,proxool-0.9.1,druid-0.2.23,spring-framework-3.2.2,hibernate-4.2.3,mybatis-3.2.2.
2.新建一个web项目,这里以之前举例项目为例,将所需要的相关jar包复制到项目的WEB-INF/lib下,这里就不详细说明各框架的jar包依赖关系了,可以自己百度查下。
3.新建接口C3P0DAO,DruidDAO,ProxoolDAO,并且添加相同的方法:
1. /**
2. * 获取用户信息
3. * @param usrId
4. * @return
5. */
6. Object getUsrInfo(Integer usrId);
4.在resource目录下新建c3p0.properties,druid.properties,proxool.properties这三个属性文件,他们分别是几种连接池的详细配置信息。
5.在resource目录下新建Spring配置文件applicationContext-dataSource.xml,内容如下:
1. <?xml versinotallow="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
4. xsi:schemaLocatinotallow="http://www.springframework.org/schema/beans
5. >
6.
7. <!-- spring加载资源文件 -->
8. <bean name="propertiesConfig"
9. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
10. <property name="locations">
11. <list>
12. <value>classpath:c3p0.properties</value>
13. <value>classpath:proxool.properties</value>
14. <value>classpath:druid.properties</value>
15. <value>classpath:hibernate.properties</value>
16. </list>
17. </property>
18. </bean>
19.
20. <!-- c3p0数据源配置 -->
21. <bean id="dataSource_c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
22. <!-- 数据库驱动 -->
23. <property name="driverClass" value="${c3p0.driverClass}" />
24. <!-- 数据库连接url -->
25. <property name="jdbcUrl" value="${c3p0.jdbcUrl}" />
26. <!-- 数据库用户 -->
27. <property name="user" value="${c3p0.user}" />
28. <!-- 数据库密码 -->
29. <property name="password" value="${c3p0.password}" />
30. <!-- 初始化时获取连接数 -->
31. <property name="initialPoolSize" value="${c3p0.initialPoolSize}" />
32. <!-- 连接池中保留的最小连接数 -->
33. <property name="minPoolSize" value="${c3p0.minPoolSize}" />
34. <!-- 连接池中保留的最大连接数 -->
35. <property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
36. </bean>
37.
38. <!-- Proxool数据源配置 -->
39. <bean id="dataSource_proxool" class="org.logicalcobwebs.proxool.ProxoolDataSource">
40. <!-- 别名 -->
41. <property name="alias" value="${proxool.alias}" />
42. <!-- 数据库驱动 -->
43. <property name="driver" value="${proxool.driver}" />
44. <!-- 数据库连接url -->
45. <property name="driverUrl" value="${proxool.driverUrl}" />
46. <!-- 数据库用户 -->
47. <property name="user" value="${proxool.user}" />
48. <!-- 数据库密码 -->
49. <property name="password" value="${proxool.password}" />
50. <!-- 最少保持的空闲连接数 -->
51. <property name="prototypeCount" value="${proxool.prototypeCount}" />
52. <!-- 最大连接数 -->
53. <property name="maximumConnectionCount" value="${proxool.maximumConnectionCount}" />
54. <!-- 最小连接数 -->
55. <property name="minimumConnectionCount" value="${proxool.minimumConnectionCount}" />
56. </bean>
57.
58. <!-- Druid数据源配置 -->
59. <bean id="dataSource_druid" class="com.alibaba.druid.pool.DruidDataSource"
60. init-method="init" destroy-method="close">
61. <!-- 数据库连接url -->
62. <property name="url" value="${druid.url}" />
63. <!-- 数据库用户 -->
64. <property name="username" value="${druid.username}" />
65. <!-- 数据库密码 -->
66. <property name="password" value="${druid.password}" />
67. <!-- 配置连接池初始化大小 -->
68. <property name="initialSize" value="${druid.initialSize}" />
69. <!-- 最小空闲连接数 -->
70. <property name="minIdle" value="${druid.minIdle}" />
71. <!-- 最大连接数 -->
72. <property name="maxActive" value="${druid.maxActive}" />
73. <!-- 获取连接等待超时的时间,单位:毫秒 -->
74. <property name="maxWait" value="${druid.maxWait}" />
75. </bean>
76.
77. </beans>
6.配置Hibernate与Spring集成,新建Spring配置文件applicationContext-hibernate.xml,内容如下:
1. <?xml versinotallow="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
4. xsi:schemaLocatinotallow="http://www.springframework.org/schema/beans
5. >
6.
7. <!-- c3p0 sessionFactory配置,注意是版本为Hibernate4 -->
8. <bean id="sessionFactory_c3p0"
9. class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
10. <!-- 将c3p0的数据源注入给hibernate使用 -->
11. <property name="dataSource" ref="dataSource_c3p0" />
12. <property name="mappingResources">
13. <list>
14. <value>com/test/jdbc/model/User.hbm.xml</value>
15. </list>
16. </property>
17. <property name="hibernateProperties">
18. <props>
19. <prop key="hibernate.dialect">${hibernate.dialect}</prop>
20. <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
21. <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
22. <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
23. <prop key="hibernate.connection.characterEncoding">${hibernate.connection.characterEncoding}</prop>
24. <prop key="hibernate.connection.autocommit">${hibernate.connection.autocommit}</prop>
25. <prop key="hibernate.connection.release_mode">${hibernate.connection.release_mode}</prop>
26. <prop key="hibernate.autoReconnect">${hibernate.autoReconnect}</prop>
27. <prop key="hibernate.cglib.use_reflection_optimizer">${hibernate.cglib.use_reflection_optimizer}</prop>
28. </props>
29. </property>
30. </bean>
31.
32. <!-- Druid sessionFactory配置,注意是版本为Hibernate4 -->
33. <bean id="sessionFactory_druid"
34. class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
35. <!-- 将c3p0的数据源注入给hibernate使用 -->
36. <property name="dataSource" ref="dataSource_druid" />
37. <property name="mappingResources">
38. <list>
39. <value>com/test/jdbc/model/User.hbm.xml</value>
40. </list>
41. </property>
42. <property name="hibernateProperties">
43. <props>
44. <prop key="hibernate.dialect">${hibernate.dialect}</prop>
45. <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
46. <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
47. <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
48. <prop key="hibernate.connection.characterEncoding">${hibernate.connection.characterEncoding}</prop>
49. <prop key="hibernate.connection.autocommit">${hibernate.connection.autocommit}</prop>
50. <prop key="hibernate.connection.release_mode">${hibernate.connection.release_mode}</prop>
51. <prop key="hibernate.autoReconnect">${hibernate.autoReconnect}</prop>
52. <prop key="hibernate.cglib.use_reflection_optimizer">${hibernate.cglib.use_reflection_optimizer}</prop>
53. </props>
54. </property>
55. </bean>
56.
57. <!-- Proxool sessionFactory配置,注意是版本为Hibernate4 -->
58. <bean id="sessionFactory_proxool"
59. class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
60. <!-- 将c3p0的数据源注入给hibernate使用 -->
61. <property name="dataSource" ref="dataSource_proxool" />
62. <property name="mappingResources">
63. <list>
64. <value>com/test/jdbc/model/User.hbm.xml</value>
65. </list>
66. </property>
67. <property name="hibernateProperties">
68. <props>
69. <prop key="hibernate.dialect">${hibernate.dialect}</prop>
70. <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
71. <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
72. <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
73. <prop key="hibernate.connection.characterEncoding">${hibernate.connection.characterEncoding}</prop>
74. <prop key="hibernate.connection.autocommit">${hibernate.connection.autocommit}</prop>
75. <prop key="hibernate.connection.release_mode">${hibernate.connection.release_mode}</prop>
76. <prop key="hibernate.autoReconnect">${hibernate.autoReconnect}</prop>
77. <prop key="hibernate.cglib.use_reflection_optimizer">${hibernate.cglib.use_reflection_optimizer}</prop>
78. </props>
79. </property>
80. </bean>
81. </beans>
7.实现我们之前定义的三个DAO接口,实现类代码比较简单我拿DruidDAOImpl为例,其他类代码相同:
1. package
2.
3. import
4. import
5. import
6.
7. /**
8. * DruidDAO实现
9. * @author g21121
10. *
11. */
12. public class DruidDAOImpl extends HibernateDaoSupport implements
13.
14. /**
15. * 获取用户信息
16. */
17. public
18. return getHibernateTemplate().get(User.class, usrId);
19. }
20.
21. }
8.新建spring配置文件applicationContext-dao.xml,将三个实现类与spring结合:
1. <?xml versinotallow="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
4. xsi:schemaLocatinotallow="http://www.springframework.org/schema/beans
5. >
6.
7. <!-- 将c3p0作为数据源的sessionFactory注入 -->
8. <bean class="com.test.jdbc.dao.impl.C3P0DAOImpl">
9. <property name="sessionFactory" ref="sessionFactory_c3p0" />
10. </bean>
11. <!-- 将Druid作为数据源的sessionFactory注入 -->
12. <bean class="com.test.jdbc.dao.impl.DruidDAOImpl">
13. <property name="sessionFactory" ref="sessionFactory_druid" />
14. </bean>
15. <!-- 将Proxool作为数据源的sessionFactory注入 -->
16. <bean class="com.test.jdbc.dao.impl.ProxoolDAOImpl">
17. <property name="sessionFactory" ref="sessionFactory_proxool" />
18. </bean>
19.
20. </beans>
这里我们将之前配置的三个数据源注入到了三个不同命名的sessionFactory,然后将三个sessionFactory分别注入到各自的DAO实现类中,这样就实现了利用不同的数据源来访问数据库。
9.新建一个测试用Servlet,代码如下:
1. package
2.
3. import
4. import
5.
6. import
7. import
8. import
9. import
10.
11. import
12.
13. import
14. import
15. import
16.
17. /**
18. * 测试Servlet
19. * @author g21121
20. *
21. */
22. public class UserServlet extends
23. /**
24. * 此处利用Spring注解自动注入属性
25. */
26. @Autowired
27. private
28. @Autowired
29. private
30. @Autowired
31. private
32.
33. /**
34. * 处理Get请求
35. */
36. public void
37. throws
38.
39. "text/html");
40. PrintWriter out = response.getWriter();
41. out
42. "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
43. "<HTML>");
44. " <HEAD><TITLE>User Servlet</TITLE></HEAD>");
45. " <BODY>");
46. "Druid:"+druidDAO.getUsrInfo(1));
47. "c3p0:"+c3P0DAO.getUsrInfo(1));
48. "Proxool:"+proxoolDAO.getUsrInfo(1));
49. " </BODY>");
50. "</HTML>");
51. out.flush();
52. out.close();
53. }
54.
55. public void
56. throws
57. this.doGet(request, response);
58. }
59.
60. public
61. super();
62. }
63. }
1. <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
2. <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
最后是项目文件,spring和hibernate包比较大所以没包含在其中。
标签:hibernate,jdbc,Spring,connection,Hibernate,import,properties,连接池 From: https://blog.51cto.com/u_16203469/6838243