枚举实现单例连接数据库

==作者:YB-Chi==

创建一个jdbc.propertis文件,其内容如下:

1
2
3
4
5
6
driverClass = com.mysql.jdbc.Driver
jdbcUrl = jdbc:mysql://localhost:3306/liaokailin
user = root
password = mysqladmin
maxPoolSize = 20
minPoolSize = 5

创建一个MyDataBaseSource的枚举:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ResourceBundle;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public enum MyDataBaseSource {
DATASOURCE;
private ComboPooledDataSource cpds = null;

private MyDataBaseSource() {
try {
/*--------获取properties文件内容------------*/

// 方法一:
/*
* InputStream is =
* MyDBSource.class.getClassLoader().getResourceAsStream("jdbc.properties");
* Properties p = new Properties(); p.load(is);
* System.out.println(p.getProperty("driverClass") );
*/

// 方法二:(不需要properties的后缀)
/*
* ResourceBundle rb = PropertyResourceBundle.getBundle("jdbc") ;
* System.out.println(rb.getString("driverClass"));
*/

// 方法三:(不需要properties的后缀)
ResourceBundle rs = ResourceBundle.getBundle("jdbc");
cpds = new ComboPooledDataSource();
cpds.setDriverClass(rs.getString("driverClass"));
cpds.setJdbcUrl(rs.getString("jdbcUrl"));
cpds.setUser(rs.getString("user"));
cpds.setPassword(rs.getString("password"));
cpds.setMaxPoolSize(Integer.parseInt(rs.getString("maxPoolSize")));
cpds.setMinPoolSize(Integer.parseInt(rs.getString("minPoolSize")));
System.out.println("-----调用了构造方法------");

} catch (Exception e) {
e.printStackTrace();
}
}

public Connection getConnection() {
try {
return cpds.getConnection();
} catch (SQLException e) {
return null;
}
}

}

测试代码:

1
2
3
4
5
6
7
public class Test {
public static void main(String[] args) {
MyDataBaseSource.DATASOURCE.getConnection() ;
MyDataBaseSource.DATASOURCE.getConnection() ;
MyDataBaseSource.DATASOURCE.getConnection() ;
}
}

结果如下:

1
2
3
-----调用了构造方法------
2013-7-17 17:10:57 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge16d8v1tgb0wppydrzz|2c1e6b, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge16d8v1tgb0wppydrzz|2c1e6b, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/kaoqin, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 5, numHelperThreads -> 3, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]

很显然获得了三个Connection连接,但是只调用了一次枚举的构造方法,从而通过枚举实现了单例的设计

文章作者: CYBSKY
文章链接: https://cybsky.top/2022/09/07/cyb-mds/java/code/枚举实现单例连接数据库/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 CYBSKY