Connection Pool Hikari

Selmau

Mitglied
Hallo Zusammen

Ich möchte über einen Hikari Connection Pool Verbindung zu meiner Datenbank Herstellen. Dies habe ich auch geschafft und die Connection steht. Das Problem ist nur, dass ich sobald ich ein PreparedStatement ausführen möchte, dies nicht möglich ist über den Hikari Connection Pool.
Ich muss irgendetwas in der Configuration des Connection Pools ändern, ich habe schon Properties für den PreparedStatementCache hinzugefügt aber es funktioniert immer noch nicht. Hat jemand von euch eine Idee wie ich dieses Problem beheben kann.

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 {
            properties = new Properties();
            properties.load(new FileInputStream("src/database.properties"));
     
            dataSource = new HikariDataSource();
          
            dataSource.setDriverClassName(properties.getProperty(DB_DRIVER_CLASS));
            dataSource.setJdbcUrl(properties.getProperty(DB_URL));
            dataSource.setUsername(properties.getProperty(DB_USERNAME));
            dataSource.setPassword(properties.getProperty(DB_PASSWORD));
           
           
             
          
            dataSource.setMinimumIdle(1);
            dataSource.setMaximumPoolSize(2000);//The maximum number of connections, idle or busy, that can be present in the pool.
           
             
            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");
           
           
          
           
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
   
    public static DataSource getDataSource(){
        return dataSource;
    }
}


Hier aus Ausführen der Connection und der SQL Query mit einem PreparedStatement:
Code:
try {
                  
                  Connection connection = null;
                 connection = pool_connection.getDataSource().getConnection();
                 String query = "INSERT INTO `produkte_snf` (`product_id`, `Produkt`, `Preis`) VALUES ('88', 'test', '26.00') ";
            
                   
                    PreparedStatement pst_insert=(PreparedStatement) connection.prepareStatement(query);
                   
                    pst_insert.execute();
                
                   
                 if (connection == null){
                     JOptionPane.showMessageDialog(null, "Keine Verbindung");
                    
                 }
                 else{
                 JOptionPane.showMessageDialog(null, "Daten gesendet");}
                   
                   
               
                   
                }catch(Exception e) {
                    JOptionPane.showMessageDialog(null, "Fehler");
                }


Freundliche Grüsse

Selmau
 

Selmau

Mitglied
Das heisst, dass wenn ich nachdem ich die Connection beschrieben habe ein PreparedStatement connection.preparedStatement aufrufe, dass dasProgramm dann immer in den catch block geht, da es den try Block nicht fertig ausführen kann.
 

mrBrown

Super-Moderator
Mitarbeiter
Lass dir doch mal im catch-Block die wirkliche Exception ausgeben.
Du fängst einfach alle, du weißt also gar nicht, was der Fehler ist, das könnte auch eine NullPointerException sein
 

Selmau

Mitglied
Der Fehler lautet von Eclipse:
java.lang.ClassCastException:com.zaxxer.hikari.pool.HikariProxyPreparedStatement cannot be cast to com.mysql.jdbc.PreparedStatement
 

Selmau

Mitglied
Genau dies möchte ich selber wissen, da ich daraus schliessen könnte, was genau ich ändern muss. Ich finde mit dem Fehlercode eine Lösung für dieses Problem.
 

mrBrown

Super-Moderator
Mitarbeiter
prepareStatement gibt dir eine java.sql.PreparedStatement zurück - das versuchst du unsinnergerweise zu com.mysql.jdbc.PreparedStatement zu casten.

Du kannst den Cast einfach weglassen und stattdessen die passenden Imports nutzen.
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
OnDemand Hikari Pool Connection Validation Datenbankprogrammierung 18
K Glassfish 4.1.1 Connection Pool u. Resource Ref Datenbankprogrammierung 20
F Monitoring DB Connection Pool Datenbankprogrammierung 3
T Pooled Connection und Connection Pool Datenbankprogrammierung 2
S Connection Pool Datenbankprogrammierung 23
R Interessantes Problem mit Connection-Pool. Datenbankprogrammierung 2
H Connection Pool + Tomcat + Oracle10g Datenbankprogrammierung 7
Maxim6394 EclipseLink + SQLite | Unable to acquire a connection from driver [null] Datenbankprogrammierung 6
J PC-Start Problem JDBC Connection Datenbankprogrammierung 10
S Oracle DB-Connection in .jar file ändern Datenbankprogrammierung 11
yakazuqi MySQL MySQL Connection reset Datenbankprogrammierung 7
Dimax MySQL Methodenaufruf mit Connection Übergabe Datenbankprogrammierung 8
D MySQL Connection richtig herstellen. Wie ? Datenbankprogrammierung 7
D Multiple Connection mit MySQL Datenbankprogrammierung 4
P MySQL Connection Global Datenbankprogrammierung 13
J Connection Datenbankprogrammierung 1
F Brauche dringend Hilfe Java-Access Connection Datenbankprogrammierung 3
S New Connection Wizard / NetBeans Datenbankprogrammierung 0
P Frage zu Connection.close() Datenbankprogrammierung 4
T NoSQL Connection für die Thesis [GWT] Datenbankprogrammierung 1
M Connection erstellen Datenbankprogrammierung 1
H JDBCODBC - Connection-Objekt Datenbankprogrammierung 3
E MySQL SQL - wann connection schließen Datenbankprogrammierung 2
R HSQLDB Connection refused Datenbankprogrammierung 2
B JDBC Connection Fehler Datenbankprogrammierung 8
B JDBC-Connection: Data source name too long Datenbankprogrammierung 3
crashfinger jdbc-connection mit jre7 funktioniert nicht Datenbankprogrammierung 5
reibi Derby/JavaDB Connection refused Datenbankprogrammierung 14
S Ressourcenverbrauch Connection Open/Close Datenbankprogrammierung 11
W MySQL-Connection-Objekt übergeben Datenbankprogrammierung 2
N SQL-Connection Datenbankprogrammierung 3
B MySQL Datenbank Connection als String zurückgeben Datenbankprogrammierung 7
M Connection Pooling Datenbankprogrammierung 7
B MySQL Fehler: Cannot open connection mit Tomcat7, Hibernate und MySQL Datenbankprogrammierung 4
K Connection - möglich & nicht möglich Datenbankprogrammierung 2
T Datenbank connection mit Servlet Datenbankprogrammierung 4
S Applet stucks at SQL Connection (jTDS JDBC) Datenbankprogrammierung 15
c_sidi90 JDBC Oracle Connection schlägt fehl Datenbankprogrammierung 2
H H2 H2-Connection bei WebStart Datenbankprogrammierung 6
JavaKaffee Derby/JavaDB Quartz-WebAnwendung - Connection/Treiber Problem Datenbankprogrammierung 47
ruutaiokwu jdbc connection als singleton Datenbankprogrammierung 11
S Wie überprüfe ich ob die Instanz einer Connection gerade werwendet wird? Datenbankprogrammierung 4
X Connection schließen oder speichern? Performance Frage Datenbankprogrammierung 7
C Derby/JavaDB JavaDB: Keine Connection Datenbankprogrammierung 7
S Java Connection to MySQL Datenbank FunPic Datenbankprogrammierung 4
Q java.lang.NullPointerException connection = null Datenbankprogrammierung 13
N Connection bleibt null Datenbankprogrammierung 7
H DB-Connection zu MySQL Datenbankprogrammierung 12
D Wie bekommt man die JDBC connection zum laufen?(Eclipse) Datenbankprogrammierung 16
T MySQL ResultSet zurückgeben nachdem Connection geschlossen wurde? Datenbankprogrammierung 3
B db2 jdbc connection Datenbankprogrammierung 4
G MySQL Connection Problem Datenbankprogrammierung 3
R sql.Connection vs. mysql.Connection Datenbankprogrammierung 3
R Connection Pooling - Tote Verbindungen Datenbankprogrammierung 5
P JPA Connection dynamisch hinzufügen Datenbankprogrammierung 2
S JDBC connection open Datenbankprogrammierung 3
D MySQL Verständnisproblem mit globalen Variablen (Connection) Datenbankprogrammierung 7
F Connection refused: connect Bei Verbindungsherstellung zu MySQL Datenbank Datenbankprogrammierung 3
R Connection Problem für eine externe DB mit Java (JDBC) Datenbankprogrammierung 9
R Connection nur als root Datenbankprogrammierung 3
N Connection kann nicht geschlossen werden!? Datenbankprogrammierung 4
S JPA Hibernate: "The user must supply a jdbc connection" Datenbankprogrammierung 4
F MySQL - Connection JDBC-Driver Problem Datenbankprogrammierung 4
E MSSQL-Server connection aufbau sehr langsam Datenbankprogrammierung 2
S Zuviele DB Connection Datenbankprogrammierung 4
A Connection Variable in anderer Klasse verwenden -> statement Datenbankprogrammierung 2
S Connection String MS Access mit Systemdatenbank / Arbeitsgruppeninformationsdatei Datenbankprogrammierung 4
R DB-Connection, aber wie? Datenbankprogrammierung 2
F Java SQL Connection mit Rollback Datenbankprogrammierung 2
P DB- Connection lösen Datenbankprogrammierung 7
padde479 Connection String Oracle Datenbankprogrammierung 5
W JDBC Connection isValid()? Datenbankprogrammierung 4
G Frage zu connection? Datenbankprogrammierung 9
G allgemeine JDBC-Connection Frage Datenbankprogrammierung 2
H Wie kann ich eine Datenbank Connection aus XML-Datei lesen! Datenbankprogrammierung 2
J jdbc Oracle Connection refused Datenbankprogrammierung 6
D Probleme mit mysql-Connection Datenbankprogrammierung 10
K Wo "Connection" Object erstellen? Datenbankprogrammierung 7
N Kleine Frage zu Connection Pooling mit DataSource Datenbankprogrammierung 2
M Hilfe - keine Connection zur DB Datenbankprogrammierung 4
G Connection zu einer Oracle DB erstellen Datenbankprogrammierung 8
K Oracle XE Connection Problem Datenbankprogrammierung 2
S Connection/Statement/ResultSet auf einmal geschlossen Datenbankprogrammierung 8
C Resultset nach connection close weiterreichen Datenbankprogrammierung 5
G SQL Server Connection Datenbankprogrammierung 12
K "Connection timed out: connect" bei MySQL-Verbindu Datenbankprogrammierung 10
R Warum ist meine Connection null? Datenbankprogrammierung 6
B Connection Pools Datenbankprogrammierung 3
U Connection läuft nicht als jar Datenbankprogrammierung 6
C Statement/Connection SQLWarning Datenbankprogrammierung 4
P Connection problems Datenbankprogrammierung 15
J Keine Connection zur MySQL Db Datenbankprogrammierung 6
K db connection wann schließen Datenbankprogrammierung 4
W Problem bei Connection mit SQLServer-Datenbanke mittels Java Datenbankprogrammierung 2
S Viele Klassen sollen eine Connection benutzen Datenbankprogrammierung 3
K Connection error Datenbankprogrammierung 18
G SQLException: No operations allowed after connection closed Datenbankprogrammierung 2
T problem mit mysql connection Datenbankprogrammierung 6
T JDBC Connection refused Problem Datenbankprogrammierung 6
L DB2 connection problem Datenbankprogrammierung 2

Ähnliche Java Themen

Neue Themen


Oben