Hall Zusammen
Ich stelle über die Erweiterung HikariCP eine Poolconnection mit einer Datenbank her.
Dafür rufe ich aus meinem Programm die unterstehende Klasse auf und erzeuge ein Objekt vom Typ HikariDataSource. Über "getConnection" baue ich die Verbindung auf. Das Ganze ist in einem try / catch Block.
Nun habe ich das Problem, dass wenn aufgrund eines Unterbruchs keine Verbindung zur Datenbank besteht, mir zwar eine Exception auf der Konsole ausgegeben wird, jedoch nicht vom Catch Block aus von dem ich den "getConnection"Befehl gebe. Ich möchte also das die Exception von der Methode getConnection weitergegeben wird, damit ich eine Aktion im Catch Block ausführen kann, falls keine VErbindung zur Datenbank besteht.
Kann mir jemand weiterhelfen, wie ich dies anstelle?
Hier mein Code:
Freundliche Grüsse
Samuel Hurni
Ich stelle über die Erweiterung HikariCP eine Poolconnection mit einer Datenbank her.
Dafür rufe ich aus meinem Programm die unterstehende Klasse auf und erzeuge ein Objekt vom Typ HikariDataSource. Über "getConnection" baue ich die Verbindung auf. Das Ganze ist in einem try / catch Block.
Nun habe ich das Problem, dass wenn aufgrund eines Unterbruchs keine Verbindung zur Datenbank besteht, mir zwar eine Exception auf der Konsole ausgegeben wird, jedoch nicht vom Catch Block aus von dem ich den "getConnection"Befehl gebe. Ich möchte also das die Exception von der Methode getConnection weitergegeben wird, damit ich eine Aktion im Catch Block ausführen kann, falls keine VErbindung zur Datenbank besteht.
Kann mir jemand weiterhelfen, wie ich dies anstelle?
Hier mein Code:
Code:
public class pool_connection {
private static final String DB_USERNAME="db.username";
private static final String DB_PASSWORD="db.password";
private static final String DB_URL ="db.url";
private static final String DB_DRIVER_CLASS="driver.class.name";
private static Properties properties = null;
private static HikariDataSource dataSource;
static {
try {
hikariConfig.addDataSourceProperty("cachePrepStmts", "true");
hikariConfig.addDataSourceProperty("prepStmtCacheSize", "250");
hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
hikariConfig.addDataSourceProperty("useServerPrepStmts", "true");
String dialect = (String) properties.get("hibernate.dialect");
if (dialect != null && dialect.contains("PostgreSQL")) {
// The JDBC jar verion in the IP BOM does not support Connection.isValid(), so need to use this:
hikariConfig.setConnectionTestQuery("SELECT 1");
}
HikariDataSource hikariDataSource = new HikariDataSource(hikariConfig);
properties.put("hibernate.connection.datasource", hikariDataSource);
*/
dataSource = new HikariDataSource();
//JOptionPane.showMessageDialog(null,"Test");
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost/mealorder");
dataSource.setUsername("test");
dataSource.setPassword("12345");
dataSource.addDataSourceProperty("cachePrepStmts", true);
// JOptionPane.showMessageDialog(null,"Test");
dataSource.setMinimumIdle(1);
dataSource.setMaximumPoolSize(2000);//The maximum number of connections, idle or busy, that can be present in the pool.
//dataSource.setAutoCommit(false);
//dataSource.setLoginTimeout(3);
dataSource.addDataSourceProperty("cachePrepStmts", "true");
dataSource.addDataSourceProperty("prepStmtCacheSize","250");
dataSource.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
dataSource.addDataSourceProperty("useServerPrepStmts", "true");
dataSource.addDataSourceProperty("useLocalSessionState","true");
dataSource.addDataSourceProperty("useLocalTransactionState","true");
dataSource.addDataSourceProperty("rewriteBatchedStatements","true");
dataSource.addDataSourceProperty("cacheResultSetMetadata","true");
dataSource.addDataSourceProperty("cacheServerConfiguration","true");
dataSource.addDataSourceProperty("elideSetAutoCommits","true");
dataSource.addDataSourceProperty("maintainTimeStats","true");
/* dataSource.addDataSourceProperty("cachePrepStmts", "true");
dataSource.addDataSourceProperty("prepStmtCacheSize", "250");
dataSource.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
dataSource.addDataSourceProperty("useServerPrepStmts", "true");
//dataSource.setInitializationFailFast(true);*/
} catch(Exception e) {
panel_Einstellungen.progressbar.getFrame().dispose();
JOptionPane.showMessageDialog(null,"Fehler beim Importieren, bitte Geräteneustart ausführen");
throw new RuntimeException();
}
}
public static DataSource getDataSource(){
return dataSource;
}
}
Freundliche Grüsse
Samuel Hurni