{"id":1875,"date":"2022-11-28T21:39:16","date_gmt":"2022-11-28T13:39:16","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1875"},"modified":"2022-11-28T21:42:44","modified_gmt":"2022-11-28T13:42:44","slug":"dbcp%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%9e%e6%8e%a5%e6%b1%a0%e7%9a%84%e4%b8%a4%e7%a7%8d%e5%ae%9e%e7%8e%b0%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1875","title":{"rendered":"DBCP\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u7684\u4e24\u79cd\u5b9e\u73b0\u65b9\u5f0f"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.P49.connection;\n\nimport java.io.File;\nimport java.io.FileInputStream;\nimport java.io.InputStream;\nimport java.sql.Connection;\nimport java.sql.SQLException;\nimport java.util.Properties;\n\nimport javax.sql.DataSource;\n\nimport org.apache.commons.dbcp2.BasicDataSource;\nimport org.apache.commons.dbcp2.BasicDataSourceFactory;\nimport org.junit.Test;\n\npublic class DBCPTest {\n\t\n\t\/**\n\t * \n\t* @Description \t\u6d4b\u8bd5DBCP\u7684\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u6280\u672f\n\t* @author RichieZhang\n\t * @throws SQLException \n\t* @date 2022\u5e7411\u670828\u65e5\u4e0b\u53487:22:43\n\t *\/\n\t\/\/\u65b9\u5f0f\u4e00\uff1a\u4e0d\u63a8\u8350\n\t@Test\n\tpublic void testGetConnection() throws SQLException {\n\t\t\/\/\u521b\u5efa\u4e86DBCP\u7684\u6570\u636e\u5e93\u8fde\u63a5\u6c60\n\t\tBasicDataSource source = new BasicDataSource();\n\t\t\n\t\t\/\/\u8bbe\u7f6e\u57fa\u672c\u4fe1\u606f\n\t\tsource.setDriverClassName(\"com.mysql.cj.jdbc.Driver\");\n\t\tsource.setUrl(\"jdbc:mysql:\/\/\/test\");\n\t\tsource.setUsername(\"root\");\n\t\tsource.setPassword(\"123456\");\n\t\t\n\t\t\/\/\u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u5176\u4ed6\u6d89\u53ca\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u7ba1\u7406\u7684\u76f8\u5173\u5c5e\u6027\uff1a\n\t\t\/\/\u8fde\u63a5\u6c60\u542f\u52a8\u65f6\u521b\u5efa\u7684\u521d\u59cb\u5316\u8fde\u63a5\u6570\u91cf\n\t\tsource.setInitialSize(10);\n\t\t\/\/v1.4 \u8fde\u63a5\u6c60\u4e2d\u53ef\u540c\u65f6\u8fde\u63a5\u7684\u6700\u5927\u7684\u8fde\u63a5\u6570\n\/\/\t\tsource.setMaxActive(10);\n\t\t\/\/v2.9 \u8fde\u63a5\u6c60\u4e2d\u53ef\u540c\u65f6\u8fde\u63a5\u7684\u6700\u5927\u7684\u8fde\u63a5\u6570\n\t\tsource.setMaxTotal(10);\n\t\t\/\/\u3002\u3002\u3002\n\t\t\n\t\tConnection conn = source.getConnection();\n\t\tSystem.out.println(conn);\n\t}\n\t\n\t\/\/\u65b9\u5f0f\u4e8c\uff1a\u63a8\u8350\uff1a\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\n\t@Test\n\tpublic void testGetConnection1() throws Exception {\n\t\tProperties pros = new Properties();\n\t\t\/\/\u65b9\u5f0f1\uff1a\n\/\/\t\tInputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream(\"dbcp.properties\");\n\t\t\/\/\u65b9\u5f0f2\uff1a\n\t\tFileInputStream is = new FileInputStream(new File(\"src\/dbcp.properties\"));\n\t\t\n\t\tpros.load(is);\n\t\t\/\/\u521b\u5efa\u4e00\u4e2aDBCP\u6570\u636e\u5e93\u8fde\u63a5\u6c60\n\t\tDataSource source = BasicDataSourceFactory.createDataSource(pros);\n\t\t\n\t\tConnection conn = source.getConnection();\n\t\tSystem.out.println(conn);\n\t\t\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p>dbcp.properties<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>driverClassName=com.mysql.cj.jdbc.Driver\nurl=jdbc:mysql:\/\/\/test\nusername=root\npassword=123456\n\ninitialSize=10<\/code><\/pre>\n\n\n\n<p>dbcp\u8fde\u63a5\u6c60\u5e38\u7528\u57fa\u672c\u914d\u7f6e\u5c5e\u6027<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dbcp\u8fde\u63a5\u6c60\u5e38\u7528\u57fa\u672c\u914d\u7f6e\u5c5e\u6027\r\n\r\n1.initialSize \uff1a\u8fde\u63a5\u6c60\u542f\u52a8\u65f6\u521b\u5efa\u7684\u521d\u59cb\u5316\u8fde\u63a5\u6570\u91cf\uff08\u9ed8\u8ba4\u503c\u4e3a0\uff09\r\n\r\n2.maxActive \uff1a\u8fde\u63a5\u6c60\u4e2d\u53ef\u540c\u65f6\u8fde\u63a5\u7684\u6700\u5927\u7684\u8fde\u63a5\u6570\uff08\u9ed8\u8ba4\u503c\u4e3a8\uff0c\u8c03\u6574\u4e3a20\uff0c\u9ad8\u5cf0\u5355\u673a\u5668\u572820\u5e76\u53d1\u5de6\u53f3\uff0c\u81ea\u5df1\u6839\u636e\u5e94\u7528\u573a\u666f\u5b9a\uff09\r\n\r\n3.maxIdle\uff1a\u8fde\u63a5\u6c60\u4e2d\u6700\u5927\u7684\u7a7a\u95f2\u7684\u8fde\u63a5\u6570\uff0c\u8d85\u8fc7\u7684\u7a7a\u95f2\u8fde\u63a5\u5c06\u88ab\u91ca\u653e\uff0c\u5982\u679c\u8bbe\u7f6e\u4e3a\u8d1f\u6570\u8868\u793a\u4e0d\u9650\u5236\uff08\u9ed8\u8ba4\u4e3a8\u4e2a\uff0cmaxIdle\u4e0d\u80fd\u8bbe\u7f6e\u592a\u5c0f\uff0c\u56e0\u4e3a\u5047\u5982\u5728\u9ad8\u8d1f\u8f7d\u7684\u60c5\u51b5\u4e0b\uff0c\u8fde\u63a5\u7684\u6253\u5f00\u65f6\u95f4\u6bd4\u5173\u95ed\u7684\u65f6\u95f4\u5feb\uff0c\u4f1a\u5f15\u8d77\u8fde\u63a5\u6c60\u4e2didle\u7684\u4e2a\u6570 \u4e0a\u5347\u8d85\u8fc7maxIdle\uff0c\u800c\u9020\u6210\u9891\u7e41\u7684\u8fde\u63a5\u9500\u6bc1\u548c\u521b\u5efa\uff0c\u7c7b\u4f3c\u4e8ejvm\u53c2\u6570\u4e2d\u7684Xmx\u8bbe\u7f6e)\r\n\r\n4.minIdle\uff1a\u8fde\u63a5\u6c60\u4e2d\u6700\u5c0f\u7684\u7a7a\u95f2\u7684\u8fde\u63a5\u6570\uff0c\u4f4e\u4e8e\u8fd9\u4e2a\u6570\u91cf\u4f1a\u88ab\u521b\u5efa\u65b0\u7684\u8fde\u63a5\uff08\u9ed8\u8ba4\u4e3a0\uff0c\u8c03\u6574\u4e3a5\uff0c\u8be5\u53c2\u6570\u8d8a\u63a5\u8fd1maxIdle\uff0c\u6027\u80fd\u8d8a\u597d\uff0c\u56e0\u4e3a\u8fde\u63a5\u7684\u521b\u5efa\u548c\u9500\u6bc1\uff0c\u90fd\u662f\u9700\u8981\u6d88\u8017\u8d44\u6e90\u7684\uff1b\u4f46\u662f\u4e0d\u80fd\u592a\u5927\uff0c\u56e0\u4e3a\u5728\u673a\u5668\u5f88\u7a7a\u95f2\u7684\u65f6\u5019\uff0c\u4e5f\u4f1a\u521b\u5efa\u4f4e\u4e8eminidle\u4e2a\u6570\u7684\u8fde\u63a5\uff0c\u7c7b\u4f3c\u4e8ejvm\u53c2\u6570\u4e2d\u7684Xmn\u8bbe\u7f6e\uff09\r\n\r\n5.maxWait  \uff1a\u6700\u5927\u7b49\u5f85\u65f6\u95f4\uff0c\u5f53\u6ca1\u6709\u53ef\u7528\u8fde\u63a5\u65f6\uff0c\u8fde\u63a5\u6c60\u7b49\u5f85\u8fde\u63a5\u91ca\u653e\u7684\u6700\u5927\u65f6\u95f4\uff0c\u8d85\u8fc7\u8be5\u65f6\u95f4\u9650\u5236\u4f1a\u629b\u51fa\u5f02\u5e38\uff0c\u5982\u679c\u8bbe\u7f6e-1\u8868\u793a\u65e0\u9650\u7b49\u5f85\uff08\u9ed8\u8ba4\u4e3a\u65e0\u9650\uff0c\u8c03\u6574\u4e3a60000ms\uff0c\u907f\u514d\u56e0\u7ebf\u7a0b\u6c60\u4e0d\u591f\u7528\uff0c\u800c\u5bfc\u81f4\u8bf7\u6c42\u88ab\u65e0\u9650\u5236\u6302\u8d77\uff09\r\n\r\n6.poolPreparedStatements\uff1a\u5f00\u542f\u6c60\u7684prepared\uff08\u9ed8\u8ba4\u662ffalse\uff0c\u672a\u8c03\u6574\uff0c\u7ecf\u8fc7\u6d4b\u8bd5\uff0c\u5f00\u542f\u540e\u7684\u6027\u80fd\u6ca1\u6709\u5173\u95ed\u7684\u597d\u3002\uff09\r\n\r\n7.maxOpenPreparedStatements\uff1a\u5f00\u542f\u6c60\u7684prepared \u540e\u7684\u540c\u65f6\u6700\u5927\u8fde\u63a5\u6570\uff08\u9ed8\u8ba4\u65e0\u9650\u5236\uff0c\u540c\u4e0a\uff0c\u672a\u914d\u7f6e\uff09\r\n\r\n8.minEvictableIdleTimeMillis  \uff1a\u8fde\u63a5\u6c60\u4e2d\u8fde\u63a5\uff0c\u5728\u65f6\u95f4\u6bb5\u5185\u4e00\u76f4\u7a7a\u95f2\uff0c \u88ab\u9010\u51fa\u8fde\u63a5\u6c60\u7684\u65f6\u95f4\r\n\r\n9.removeAbandonedTimeout  \uff1a\u8d85\u8fc7\u65f6\u95f4\u9650\u5236\uff0c\u56de\u6536\u6ca1\u6709\u7528(\u5e9f\u5f03)\u7684\u8fde\u63a5\uff08\u9ed8\u8ba4\u4e3a 300\u79d2\uff0c\u8c03\u6574\u4e3a180\uff09\r\n\r\n10.removeAbandoned  \uff1a\u8d85\u8fc7removeAbandonedTimeout\u65f6\u95f4\u540e\uff0c\u662f\u5426\u8fdb \u884c\u6ca1\u7528\u8fde\u63a5\uff08\u5e9f\u5f03\uff09\u7684\u56de\u6536\uff08\u9ed8\u8ba4\u4e3afalse\uff0c\u8c03\u6574\u4e3atrue)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>dbcp.properties dbcp\u8fde\u63a5\u6c60\u5e38\u7528\u57fa\u672c\u914d\u7f6e\u5c5e\u6027<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[48],"class_list":["post-1875","post","type-post","status-publish","format-standard","hentry","category-jdbc-code","tag-jdbc"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1875"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1875"}],"version-history":[{"count":3,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1875\/revisions"}],"predecessor-version":[{"id":1883,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1875\/revisions\/1883"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}