简体   繁体   中英

Unable to create a connection pool in Java + MySQL + Vibur-dbcp

I am trying to create a conection pool whit vibur-dbcp for an app for desktop in JavaFX , i got a MySQL database in AWS and works great when i connect from Workbench butt when i connect from the app the response time increments, i check my code and the reason is because the app creates to many connections creating to much overheat (firts the app have a embedded SQLite databese and the conections weren't a problem) i remake the estructure of my code butt i still need to make alot of conections.

looking for a solution i find that conection pool is the way, i find a good number of solutions Apache commons, C3P0, HikariCP and Vibur DBCP i try to use all but at the moment i can't make one to work, i look for a tutorial but i only can find old implementations (Java 6 or older) for servlets and only snippets and all are very confusing for me (im a begginer in DB), my favorite option is HikariCP but i can make the pool to work, my next option is Vibur at the moment i have this code for the conection.

private Connection connection;

//creates the pool
public DataSource createDataSourceWithStatementsCache() {
 ViburDBCPDataSource ds = new ViburDBCPDataSource();

 ds.setJdbcUrl("dbURL");
 ds.setUsername("dbUser");
 ds.setPassword("dbPass");

 ds.setPoolInitialSize(10);
 ds.setPoolMaxSize(100);

 ds.setConnectionIdleLimitInSeconds(30);
 ds.setTestConnectionQuery("isValid");

 ds.setLogQueryExecutionLongerThanMs(500);
 ds.setLogStackTraceForLongQueryExecution(true);

 ds.setStatementCacheMaxSize(200);

 ds.start();
 return ds;
}

//Getts the conection
public Connection conectarBD(){
    try {
        connection = createDataSourceWithStatementsCache().getConnection();
    } catch (SQLException ex) {
        Logger.getLogger(coneccionBD.class.getName()).log(Level.SEVERE, null, ex);
    }
   return connection;
}


public void desconectarBD(){
    try {
        connection.close();
    } catch (SQLException ex) {
        Logger.getLogger(coneccionBD.class.getName()).log(Level.SEVERE, null, ex);
    }
}

i get this error

Exception in thread "JavaFX Application Thread" java.lang.NoClassDefFoundError: org/vibur/objectpool/listener/Listener
    at HE.MVC.Modelo.Laboratoriales.Conexion.createDataSourceWithStatementsCache(Conexion.java:36)
    at HE.MVC.Modelo.Laboratoriales.Conexion.conectarBD(Conexion.java:63)
    at HE.MVC.Modelo.MedicamentosAbituales.cargaTabla(MedicamentosAbituales.java:186)
    at HE.MVC.Vistas.Paciente.NuevopacienteController.iniciaTablas(NuevopacienteController.java:351)
    at HE.MVC.Vistas.Paciente.NuevopacienteController.initialize(NuevopacienteController.java:1156)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at HE.HEstadistica.LanzaNuevoPaciente(HEstadistica.java:304)
    at HE.MVC.Vistas.Paciente.EscenaPacientesController$7$1.run(EscenaPacientesController.java:347)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.vibur.objectpool.listener.Listener
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 17 more

what im asking is two things

1.- what i making wrong in this code?.

2.- is there a book, manual, page o videos for learning conecction pool (HikariCP explanation for begginers it would be wonderful) where i can learn how to make this work?

@CorrOrtiz, regarding your first question, what @hotzst suggests in his comment below your question is correct. For Vibur DBCP there are two jar files that you need as dependencies for your application: vibur-dbcp-9.0.jar and vibur-object-pool-9.0.jar. The exception that you're getting means that you're most likely missing the second dependency, although if you've added the vibur-dbcp dependency via Maven then you should have transitively got the second dependency, too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM