|
|
|
|
@ -5,6 +5,8 @@ import org.apache.logging.log4j.Logger;
|
|
|
|
|
import org.leolo.nrdatad.ConfigurationManager; |
|
|
|
|
import org.mariadb.jdbc.MariaDbPoolDataSource; |
|
|
|
|
|
|
|
|
|
import java.sql.*; |
|
|
|
|
|
|
|
|
|
public class DatabaseManager implements org.leolo.nrdatad.db.DatabaseManager{ |
|
|
|
|
Logger logger = LogManager.getLogger(); |
|
|
|
|
private MariaDbPoolDataSource ds; |
|
|
|
|
@ -26,5 +28,33 @@ public class DatabaseManager implements org.leolo.nrdatad.db.DatabaseManager{
|
|
|
|
|
":"+conf.getProperty("db.port", "3306")+ |
|
|
|
|
"/"+conf.getProperty("db.name"); |
|
|
|
|
logger.atDebug().log("URL={}",url); |
|
|
|
|
try { |
|
|
|
|
ds = new MariaDbPoolDataSource(url); |
|
|
|
|
ds.setMinPoolSize(1); |
|
|
|
|
ds.setMaxPoolSize(Integer.parseInt(conf.getOrDefault("db.poolsize", "20").toString())); |
|
|
|
|
ds.setUser(conf.getProperty("db.user").toString()); |
|
|
|
|
ds.setPassword(conf.getProperty("db.pwd").toString()); |
|
|
|
|
} catch (SQLException e) { |
|
|
|
|
logger.atFatal().withThrowable(e).log("Cannot connect to DB"); |
|
|
|
|
System.exit(-2); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean checkConnection() { |
|
|
|
|
try( |
|
|
|
|
Connection conn = ds.getConnection(); |
|
|
|
|
Statement stmt = conn.createStatement(); |
|
|
|
|
ResultSet rs = stmt.executeQuery("SELECT 1") |
|
|
|
|
){ |
|
|
|
|
return rs.next(); |
|
|
|
|
}catch(SQLException e){ |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Connection getConnection() throws SQLException{ |
|
|
|
|
return ds.getConnection(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|