摘要:本文主要向大家介绍了MFC编程实例:从零实现MVC框架之数据库连接池(2),通过具体的内容向大家展示,希望对大家学习MFC编程实例有所帮助。
本文主要向大家介绍了MFC编程实例:从零实现MVC框架之数据库连接池(2),通过具体的内容向大家展示,希望对大家学习MFC编程实例有所帮助。
使用mysql数据库,JDK1.7。
数据库配置
config_db.properties文件放到src下,如下:
[plain] view plain copy
1. #数据库配置
2. driver=com.mysql.jdbc.Driver
3. url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
4. username=***
5. password=*****
6. poolsize=20
7. autocommit=true
读取配置的类,如下:
[java] view plain copy
1. package com.hc.pool;
2.
3. import java.io.FileInputStream;
4. import java.io.IOException;
5. import java.util.Properties;
6. /**
7. *
8. * @author chuer
9. * @date 2014-7-15 下午3:35:37
10. * @version V1.0
11. */
12. public class DBConfig {
13.
14. public static int MAX_POOL_SIZE = 20;
15. public static String URL = "";
16. public static String DRIVER = "";
17. public static String USERNAME = "";
18. public static String PASSWORD = "";
19. public static boolean AUTOCOMMIT = true;
20.
21. static{
22. //读取配置文件
23. Properties prop = new Properties();
24. try {
25. String path = DBConfig.class.getResource("/").getPath();
26. prop.load(new FileInputStream(path+"config_db.properties"));
27. DRIVER = (String)prop.get("driver");
28. URL = (String)prop.get("url");
29. USERNAME = (String)prop.get("username");
30. PASSWORD = (String)prop.get("password");
31. AUTOCOMMIT = Boolean.parseBoolean(prop.get("autocommit").toString());
32. MAX_POOL_SIZE = Integer.parseInt(prop.get("poolsize").toString());
33. } catch (IOException e) {
34. e.printStackTrace();
35. }
36. }
37.
38. public static void main(String...args){
39.
40. }
41. }
连接管理类
代码如下:
[java] view plain copy
1. package com.hc.pool;
2.
3. import java.sql.Connection;
4. import java.sql.DriverManager;
5. import java.sql.SQLException;
6. /**
7. *
8. * @author chuer
9. * @date 2014-7-15 下午3:35:37
10. * @version V1.0
11. */
12. public class DBManager {
13. public static void initPool(){
14. try {
15. Class.forName(DBConfig.DRIVER);
16. for(int i=0;i<DBConfig.MAX_POOL_SIZE;i++){
17. Connection connection = getConnection();
18. connection.setAutoCommit(false);
19. DBPool.getInstance().addConnection(connection);
20. }
21. } catch (ClassNotFoundException e) {
22. e.printStackTrace();
23. } catch (SQLException e) {
24. e.printStackTrace();
25. }
26. }
27.
28. public static Connection getConnection() {
29. Connection conn = null;
30. try {
31. conn = DriverManager.getConnection(DBConfig.URL, DBConfig.USERNAME, DBConfig.PASSWORD);
32. } catch (SQLException e) {
33. e.printStackTrace();
34. }
35. return conn;
36. }
37. }
连接池
接口:
[java] view plain copy
1. package com.hc.core;
2.
3. import java.sql.Connection;
4. /**
5. *
6. * @author chuer
7. * @date 2014-7-15 下午6:40:24
8. * @version V1.0
9. */
10. public interface IPool {
11.
12.
13. public Connection getConnection();
14.
15. public void relaseConnection(Connection conn);
16. }
实现类:
[java] view plain copy
1. package com.hc.pool;
2.
3. import java.sql.Connection;
4. import java.sql.SQLException;
5. import java.util.concurrent.ConcurrentLinkedQueue;
6.
7. import com.hc.core.IPool;
8. /**
9. *
10. * @author chuer
11. * @date 2014-7-15 下午3:35:37
12. * @version V1.0
13. */
14. public class DBPool implements IPool{
15. private static volatile boolean isInit = false;
16. private DBPool(){}
17. private static DBPool dbPool = new DBPool();
18. private ConcurrentLinkedQueue<Connection> pools = new ConcurrentLinkedQueue<Connection>();
19.
20. public static DBPool getInstance(){
21. if(!isInit){
22. isInit = true;
23. dbPool.init();
24. }
25. return dbPool;
26. }
27.
28. public void init(){
29. DBManager.initPool();
30. }
31.
32. @Override
33. public Connection getConnection() {
34. Connection conn = pools.poll();
35. if(conn == null){
36. conn = DBManager.getConnection();
37. }
38. return conn;
39. }
40.
41. @Override
42. public void relaseConnection(Connection conn) {
43. try {
44. if(!conn.isClosed()){
45. pools.offer(conn);
46. }else{
47. Connection connection = DBManager.getConnection();
48. pools.offer(connection);
49. }
50. } catch (SQLException e) {
51. e.printStackTrace();
52. }
53. }
54.
55. void addConnection(Connection conn){
56. pools.offer(conn);
57. }
58.
59. }
测试
[java] view plain copy
1. package com.hc.pool;
2.
3. import java.sql.Connection;
4. import java.sql.SQLException;
5.
6. public class DBTest {
7.
8. /**
9. * @param args
10. * @throws SQLException
11. */
12. public static void main(String[] args) throws SQLException {
13.
14. for(int i=1;i<=40;i++){
15. //取得连接
16. Connection connection = DBPool.getInstance().getConnection();
17.
18. //业务处理
19. // System.out.println(connection.isClosed());
20. System.out.println(connection+" "+i);
21.
22. //释放连接
23. DBPool.getInstance().relaseConnection(connection);
24. }
25.
26.
27. }
28.
29. }
运行测试类,输出如下:
[plain] view plain copy
1. com.mysql.jdbc.JDBC4Connection@14174f9 1
2. com.mysql.jdbc.JDBC4Connection@669255 2
3. com.mysql.jdbc.JDBC4Connection@114a41 3
4. com.mysql.jdbc.JDBC4Connection@1220b15 4
5. com.mysql.jdbc.JDBC4Connection@5966f 5
6. com.mysql.jdbc.JDBC4Connection@d76d1e 6
7. com.mysql.jdbc.JDBC4Connection@13ce184 7
8. com.mysql.jdbc.JDBC4Connection@1f9cdda 8
9. com.mysql.jdbc.JDBC4Connection@139d369 9
10. com.mysql.jdbc.JDBC4Connection@166b0df 10
11. com.mysql.jdbc.JDBC4Connection@e62a39 11
12. com.mysql.jdbc.JDBC4Connection@de1b36 12
13. com.mysql.jdbc.JDBC4Connection@1cc946b 13
14. com.mysql.jdbc.JDBC4Connection@2423ad 14
15. com.mysql.jdbc.JDBC4Connection@a26638 15
16. com.mysql.jdbc.JDBC4Connection@1e4e6db 16
17. com.mysql.jdbc.JDBC4Connection@f0474c 17
18. com.mysql.jdbc.JDBC4Connection@d8fb2b 18
19. com.mysql.jdbc.JDBC4Connection@151e135 19
20. com.mysql.jdbc.JDBC4Connection@9875d1 20
21. com.mysql.jdbc.JDBC4Connection@14174f9 21
22. com.mysql.jdbc.JDBC4Connection@669255 22
23. com.mysql.jdbc.JDBC4Connection@114a41 23
24. com.mysql.jdbc.JDBC4Connection@1220b15 24
25. com.mysql.jdbc.JDBC4Connection@5966f 25
26. com.mysql.jdbc.JDBC4Connection@d76d1e 26
27. com.mysql.jdbc.JDBC4Connection@13ce184 27
28. com.mysql.jdbc.JDBC4Connection@1f9cdda 28
29. com.mysql.jdbc.JDBC4Connection@139d369 29
30. com.mysql.jdbc.JDBC4Connection@166b0df 30
31. com.mysql.jdbc.JDBC4Connection@e62a39 31
32. com.mysql.jdbc.JDBC4Connection@de1b36 32
33. com.mysql.jdbc.JDBC4Connection@1cc946b 33
34. com.mysql.jdbc.JDBC4Connection@2423ad 34
35. com.mysql.jdbc.JDBC4Connection@a26638 35
36. com.mysql.jdbc.JDBC4Connection@1e4e6db 36
37. com.mysql.jdbc.JDBC4Connection@f0474c 37
38. com.mysql.jdbc.JDBC4Connection@d8fb2b 38
39. com.mysql.jdbc.JDBC4Connection@151e135 39
40. com.mysql.jdbc.JDBC4Connection@9875d1 40
从输出我们看到,1-20 和 21-40的对象是相等的。因为我们连接池的数量是20。需要注意的是getConnection方法和relaseConnection方法是成对出现的。从连接池取得的连接必须释放。
以上就介绍了VC/MFC的学习,希望对VC/MFC有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言VC/MFC频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号