java的单例模式实现从配置文件获取信息和单例实现mysql的数据库连接池
<svg xmlns="http://www.w3.org/2000/svg" style="display: none">
<path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0)"></path>
</svg>
<h5><a id="java_0"></a>java的单例模式实现从配置文件获取信息<button class="cnblogs-toc-button" title="显示目录导航" aria-expanded="false"></button></h5>
package lm;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class ConfigFile {
static public Properties p = new Properties();
private static ConfigFile singleton = null;
static public ConfigFile getInstance() {
if (singleton == null) {
synchronized(ConfigFile.class) {
if (singleton == null) {
singleton = new ConfigFile();
}
}
}
return singleton;
}
public void init(String path) throws FileNotFoundException, IOException {
p.load(new FileInputStream(path));
}
public String getMysqlUrl() {
return p.getProperty("MYSQL_URL");
}
public String getMYSQL_USER() {
return p.getProperty("MYSQL_USER");
}
public String getMYSQL_PWD() {
return p.getProperty("MYSQL_PWD");
}
}
java 单例实现 mysql 的数据库连接池
package lm;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import up.conf.ConfigFile;
public class ConnectionPool {
Logger LOG = LogManager.getLogger(ConnectionPool.class);
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String JDBC_DB_URL = ConfigFile.getInstance().getMysqlUrl();
<span class="hljs-title class_">String</span> <span class="hljs-variable constant_">JDBC_USER</span> = <span class="hljs-title class_">ConfigFile</span>.<span class="hljs-title function_">getInstance</span>().<span class="hljs-title function_">getMYSQL_USER</span>();
<span class="hljs-title class_">String</span> <span class="hljs-variable constant_">JDBC_PASS</span> = <span class="hljs-title class_">ConfigFile</span>.<span class="hljs-title function_">getInstance</span>().<span class="hljs-title function_">getMYSQL_PWD</span>();
<span class="hljs-title class_">List</span><<span class="hljs-title class_">Connection</span>> cs = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span><<span class="hljs-title class_">Connection</span>>();
private <span class="hljs-keyword">static</span> <span class="hljs-title class_">ConnectionPool</span> singleton = <span class="hljs-literal">null</span>;
int size;
<span class="hljs-keyword">static</span> public <span class="hljs-title class_">ConnectionPool</span> <span class="hljs-title function_">getInstance</span>(<span class="hljs-params"></span>) {
<span class="hljs-keyword">if</span> (singleton == <span class="hljs-literal">null</span>) {
<span class="hljs-title function_">synchronized</span>(<span class="hljs-params">ConnectionPool.<span class="hljs-keyword">class</span></span>) {
<span class="hljs-keyword">if</span> (singleton == <span class="hljs-literal">null</span>) {
singleton = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ConnectionPool</span>(<span class="hljs-number">10</span>);
}
}
}
<span class="hljs-keyword">return</span> singleton;
}
private <span class="hljs-title class_">ConnectionPool</span>(int size) {
<span class="hljs-variable language_">this</span>.<span class="hljs-property">size</span> = size;
<span class="hljs-title function_">init</span>();
}
public <span class="hljs-keyword">void</span> <span class="hljs-title function_">init</span>(<span class="hljs-params"></span>) {
<span class="hljs-comment">// 这里恰恰不能使用try-with-resource的方式,因为这些连接都需要是"活"的,不要被自动关闭了</span>
<span class="hljs-keyword">try</span> {
<span class="hljs-title class_">Class</span>.<span class="hljs-title function_">forName</span>(<span class="hljs-variable constant_">JDBC_DRIVER</span>);
<span class="hljs-keyword">for</span> (int i = <span class="hljs-number">0</span>; i < size; i++) {
<span class="hljs-title class_">Connection</span> c = <span class="hljs-title class_">DriverManager</span>.<span class="hljs-title function_">getConnection</span>(<span class="hljs-variable constant_">JDBC_DB_URL</span>,<span class="hljs-variable constant_">JDBC_USER</span>, <span class="hljs-variable constant_">JDBC_PASS</span>);
<span class="hljs-comment">// System.out.println("完成第"+ ( i + 1 ) +"个连接");</span>
cs.<span class="hljs-title function_">add</span>(c);
}
} <span class="hljs-keyword">catch</span> (<span class="hljs-title class_">ClassNotFoundException</span> e) {
<span class="hljs-variable constant_">LOG</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">"init connectPool: "</span>,e);
<span class="hljs-comment">//System.out.println("init connectPool: " + e.getMessage());</span>
} <span class="hljs-keyword">catch</span> (<span class="hljs-title class_">SQLException</span> e) {
<span class="hljs-variable constant_">LOG</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">"init connectPool: "</span>,e);
<span class="hljs-comment">//System.out.println("init connectPool: " + e.getMessage());</span>
}
}
public synchronized <span class="hljs-title class_">Connection</span> <span class="hljs-title function_">getConnection</span>(<span class="hljs-params"></span>) {
<span class="hljs-keyword">while</span> (cs.<span class="hljs-title function_">isEmpty</span>()) {
<span class="hljs-keyword">try</span> {
<span class="hljs-comment">// 如果连接池中没有连接可用则等待</span>
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">wait</span>();
} <span class="hljs-keyword">catch</span> (<span class="hljs-title class_">InterruptedException</span> e) {
<span class="hljs-comment">//System.out.println("getConnection get error:" + e.getMessage());</span>
<span class="hljs-variable constant_">LOG</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">"getConnection get error:"</span>,e);
}
}
<span class="hljs-comment">// 移除第一个连接池</span>
<span class="hljs-title class_">Connection</span> conn = cs.<span class="hljs-title function_">remove</span>(<span class="hljs-number">0</span>);
<span class="hljs-keyword">return</span> conn;
}
public synchronized <span class="hljs-keyword">void</span> <span class="hljs-title function_">returnConnection</span>(<span class="hljs-params">Connection c</span>) {
<span class="hljs-comment">// 使用完给回连接池中</span>
cs.<span class="hljs-title function_">add</span>(c);
<span class="hljs-comment">// 唤醒wait告诉它有新的连接池可用</span>
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">notifyAll</span>();
}
}