Hikari SQL Poolconnection catch Exeption

Selmau

Mitglied
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:
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
 

mrBrown

Super-Moderator
Mitarbeiter
Wenn ich dich richtig verstehe, möchtest du, dass die Exception, die im statischen Initializer geworfen und gefangen wird, in irgendeiner getConnection geworfen wird?
 

Selmau

Mitglied
Ich möchte, dass wenn ich das Objekt erzeuge und danach die Verbindung mit der Methode getConnection aufbaue, und dies nicht möglich ist, ich in den Catch Block gelange und dort dann eine Aktion ausführen kann, beispielsweise eine Meldung über das Swing GUI anzeigen.

Den Befehl getConnection ist jedoch vom HikariCP Paket und ich nehme an, dass diese Methode die Exeption nicht weitergibt sondern darin die Exception auf die Konsole ausgegeben wird. Somit kann ich die Exception nicht mit meinem Catch block ausserhalb abfangen.
 

Selmau

Mitglied
Hier ist der Teil vom Code, wo ich das Objekt erstelle und die Verbindung aufbauen möchte.

Code:
public class import_data2 implements Runnable   {
    public boolean conn_sucessful= false;
    Connection conn = null;
    
    public void run() throws RuntimeException {
        
    
            pool_connection connection = new pool_connection();
            try {
                conn = connection.getDataSource().getConnection();
            } catch (SQLException e) {
                JOptionPane.showMessageDialog(null,"Fehler");
                //throw new RuntimeException();
                // TODO Auto-generated catch block
                
            }
 

mrBrown

Super-Moderator
Mitarbeiter
Ich möchte, dass wenn ich das Objekt erzeuge
Welches Objekt?

Den Befehl getConnection ist jedoch vom HikariCP Paket und ich nehme an, dass diese Methode die Exeption nicht weitergibt sondern darin die Exception auf die Konsole ausgegeben wird. Somit kann ich die Exception nicht mit meinem Catch block ausserhalb abfangen.
getConnection sollte eine SqlExcepion schmeißen, wenn da was schief geht.

Was für eine Exception wird denn mit welchem Stacktrace auf der Konsole ausgegeben?
 

Selmau

Mitglied
Das hier wird auf der Konsole ausgegeben:

Code:
Exception in thread "Thread-3" com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:516)
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:118)
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:94)
    at import_data2.run(import_data2.java:50)
    at java.lang.Thread.run(Unknown Source)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:989)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:341)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2251)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:95)
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:101)
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:316)
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:506)
    ... 4 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
    ... 20 more
 

Ähnliche Java Themen

Neue Themen


Oben