我正在学习使用 HikariCP(我是 java 新手),我发现了一个包装器,但我认为它不是线程安全的,单例的实例是线程安全的,但不是 getConnection() 方法。课程是这样的:
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经验,所以我需要你的建议。一流(原始)线程安全吗?我的实现线程安全吗?