DataSourceHandler.java 文件源码

java
阅读 25 收藏 0 点赞 0 评论 0

项目:BungeeAdminTools 作者:
/**
 * Constructor used for MySQL
 * 
 * @param host
 * @param port
 * @param database
 * @param username
 * @param password
 * @throws SQLException 
 */
public DataSourceHandler(final String host, final String port, final String database, final String username, final String password) throws SQLException{
    // Check database's informations and init connection
    this.host = Preconditions.checkNotNull(host);
    this.port = Preconditions.checkNotNull(port);
    this.database = Preconditions.checkNotNull(database);
    this.username = Preconditions.checkNotNull(username);
    this.password = Preconditions.checkNotNull(password);

    BAT.getInstance().getLogger().config("Initialization of HikariCP in progress ...");
    BasicConfigurator.configure(new NullAppender());
    ds = new HikariDataSource();
    ds.setJdbcUrl("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + 
            "?useLegacyDatetimeCode=false&serverTimezone=" + TimeZone.getDefault().getID());
    ds.setUsername(this.username);
    ds.setPassword(this.password);
    ds.addDataSourceProperty("cachePrepStmts", "true");
    ds.setMaximumPoolSize(8);
    try {
        final Connection conn = ds.getConnection();
        int intOffset = Calendar.getInstance().getTimeZone().getOffset(Calendar.getInstance().getTimeInMillis()) / 1000;
        String offset = String.format("%02d:%02d", Math.abs(intOffset / 3600), Math.abs((intOffset / 60) % 60));
        offset = (intOffset >= 0 ? "+" : "-") + offset;
        conn.createStatement().executeQuery("SET time_zone='" + offset + "';");
        conn.close();
        BAT.getInstance().getLogger().config("BoneCP is loaded !");
    } catch (final SQLException e) {
        BAT.getInstance().getLogger().severe("BAT encounters a problem during the initialization of the database connection."
                + " Please check your logins and database configuration.");
        if(e.getCause() instanceof CommunicationsException){
            BAT.getInstance().getLogger().severe(e.getCause().getMessage());
        }
        if(BAT.getInstance().getConfiguration().isDebugMode()){
            BAT.getInstance().getLogger().log(Level.SEVERE, e.getMessage(), e);
        }
        throw e;
    }
    sqlite = false;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号