gpt4 book ai didi

java - HikariCP 包装器(线程安全)

转载 作者:行者123 更新时间:2023-12-01 22:21:08 25 4
gpt4 key购买 nike

我正在学习使用HikariCP(我是java新手),我找到了一个包装器,但我认为它不是线程安全的,单例的实例是线程安全的,但不是方法getConnection()。类(class)是这样的:

public class HikariCPWrapper{
private static final HikariCPWrapper INSTANCE;
private HikariDataSource ds;
static
{
INSTANCE = new HikariCPWrapper();
}

private HikariCPWrapper(){
HikariConfig config = new HikariConfig();
//config.set...
//...
ds = new HikariDataSource(config);

}

public static HikariCPWrapper getInstance ()
{
return INSTANCE;
}

public Connection getConnection() throws SQLException
{
return ds.getConnection();
}
}

嗯,我需要将参数发送到 HikariConfig 或 HikariDataSource,所以我以这种方式重写它:

public interface IConnectionProvider {
void init(String jdbcUrl, String user, String password);
Connection getConnection() throws SQLException;
}

public class ConnectionProviderHikariCP implements IConnectionProvider{

private static final ConnectionProviderHikariCP INSTANCE;
private final HikariDataSource hikariDataSource;
private Boolean initialized;
//class initializer:
static
{
INSTANCE = new ConnectionProviderHikariCP();
}

private ConnectionProviderHikariCP() {
hikariDataSource = new HikariDataSource();
initialized = false;
}

public static ConnectionProviderHikariCP getInstance() {
return INSTANCE;
}

@Override
public synchronized void init(String jdbcUrl, String user, String password) {
hikariDataSource.setJdbcUrl(jdbcUrl);
hikariDataSource.setUsername(user);
hikariDataSource.setPassword(password);
initialized = true;
}

@Override
public synchronized Connection getConnection() throws SQLException {
if(!initialized)
throw new UnsupportedOperationException("Debe inicializar mediante el método Init() primero!!!!!.");

return hikariDataSource.getConnection();
}

}

我这样使用它:

IConnectionProvider connectionProvider = ConnectionProviderHikariCP.getInstance();
connectionProvider.init(url, user, passwd);

BaseDAOFactory fatory = new MySqlDAOFactory(connectionProvider);
IExerciseBO exerciseBO = new ExerciseBO(fatory);

但是我没有java经验,所以我需要你的建议。第一类(原始)线程安全吗?我的实现线程安全吗?

最佳答案

HikariCP 本身是线程安全的。据我了解,您想要做的是阻止调用者 getConnection()之前HikariDataSource已通过 init() 初始化方法。想必您还想防止多次调用 init()数据源。

包装getConnection()就池吞吐量而言,使用同步通常是一个坏主意。根据您上面的代码,我推荐以下模式:

public class ConnectionProviderHikariCP implements IConnectionProvider {

private static final ConnectionProviderHikariCP INSTANCE;
private final HikariDataSource hikariDataSource;
private AtomicBoolean initialized;
//class initializer:
static
{
INSTANCE = new ConnectionProviderHikariCP();
}

private ConnectionProviderHikariCP() {
hikariDataSource = new HikariDataSource();
initialized = new AtomicInteger();
}

public static ConnectionProviderHikariCP getInstance() {
return INSTANCE;
}

@Override
public void init(String jdbcUrl, String user, String password) {
if (initialized.compareAndSet(false, true)) {
hikariDataSource.setJdbcUrl(jdbcUrl);
hikariDataSource.setUsername(user);
hikariDataSource.setPassword(password);
}
else {
throw new IllegalStateException("Connection provider already initialized.");
}
}

@Override
public Connection getConnection() throws SQLException {
if (initialized.get()) {
return hikariDataSource.getConnection();
}

throw new UnsupportedOperationException("Debe inicializar mediante el método Init() primero!!!!!.");
}
}

关于java - HikariCP 包装器(线程安全),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29738151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com