/
HikariCPConnectionPoolFactory.java
98 lines (85 loc) · 3.42 KB
/
HikariCPConnectionPoolFactory.java
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**********************************************************************
Copyright (c) 2014 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contributors:
...
**********************************************************************/
package org.datanucleus.store.rdbms.connectionpool;
import javax.sql.DataSource;
import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.store.StoreManager;
import org.datanucleus.store.rdbms.RDBMSPropertyNames;
import org.datanucleus.util.ClassUtils;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
/**
* ConnectionFactory for HikariCP pools.
* See https://github.com/brettwooldridge/HikariCP
*/
public class HikariCPConnectionPoolFactory extends AbstractConnectionPoolFactory
{
/* (non-Javadoc)
* @see org.datanucleus.store.rdbms.datasource.ConnectionPoolFactory#createConnectionPool(org.datanucleus.store.StoreManager)
*/
public ConnectionPool createConnectionPool(StoreManager storeMgr)
{
String dbDriver = storeMgr.getConnectionDriverName();
String dbURL = storeMgr.getConnectionURL();
String dbUser = storeMgr.getConnectionUserName();
if (dbUser == null)
{
dbUser = ""; // Some RDBMS (e.g Postgresql) don't like null usernames
}
String dbPassword = storeMgr.getConnectionPassword();
if (dbPassword == null)
{
dbPassword = ""; // Some RDBMS (e.g Postgresql) don't like null passwords
}
// Load the database driver
ClassLoaderResolver clr = storeMgr.getNucleusContext().getClassLoaderResolver(null);
loadDriver(dbDriver, clr);
// Check the existence of the necessary pooling classes
ClassUtils.assertClassForJarExistsInClasspath(clr, "com.zaxxer.hikari.HikariConfig", "hikaricp.jar");
HikariConfig config = new HikariConfig();
config.setDriverClassName(dbDriver);
config.setJdbcUrl(dbURL);
config.setUsername(dbUser);
config.setPassword(dbPassword);
if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_POOL_SIZE))
{
int size = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_POOL_SIZE);
if (size >= 0)
{
config.setMaximumPoolSize(size);
}
}
// TODO Support other properties
// Create the actual pool of connections
HikariDataSource ds = new HikariDataSource(config);
return new HikariCPConnectionPool(ds);
}
public class HikariCPConnectionPool implements ConnectionPool
{
final HikariDataSource dataSource;
public HikariCPConnectionPool(HikariDataSource ds)
{
dataSource = ds;
}
public void close()
{
dataSource.close();
}
public DataSource getDataSource()
{
return dataSource;
}
}
}