Last Updated on 17 July 2019   |   Print Email
In this Java tutorial, we will guide you how to use the database connection pooling library Proxool for your existing Hibernate/JPA projects. Hibernate provides built-in support for Proxool via its ProxoolConnectionProvider class, and it requires just some small XML code to make it works with Proxool.
1. Get Proxool JAR files
If you project uses Maven, add the following dependency declaration to the pom.xml file:
It will download 2 JAR files proxool-0.9.1.jar and proxool-cglib.jar and add them to the project’s dependencies.In case you don’t use Maven or any build system, go to Proxool Download Page to download the proxool-0.9.1.zip file. Extract it, go to the lib directory, and put the two mentioned JAR files to your project.
2. Configure Proxool with Hibernate (hibernate.cfg.xml)
Normally, the Hibernate configuration file looks like this - with a section that specifies database connection information:
Proxool requires its own configuration file, so create the proxool.xml file in the same directory as the hibernate.cfg.xml file with the following content:
As you can see, Proxool requires the following information:
- Alias name for the pool- Database connection information such as database URL, JDBC driver, username, and password.- Configuration properties for the pool. There are 2 fundamental properties you need to specify:+ minimum-connection-count: the minimum number of connections that are kept open.+ maximum-connection-count: the maximum number of connections to the database.You can see the full list of Proxool properties here. Therefore, in the Hibernate configuration file, you should remove the section about database connection information, and specify the alias name and XML file name for Proxool like this:
Note that the proxool.xml file must be in the same directory as the hibernate.cfg.xml file like this:Then you’re all set of configuring Proxool with Hibernate in your project.
3. Configure Proxool with JPA (persistence.xml)
If you use JPA with Hibernate implementation, the database connection information is specified in the persistence.xmlfile like this:
To configure Proxool with JPA, create the proxool.xml file as described in the previous section - but it should be placed in the root of the source folder, not in the same location as the persistence.xml file:The modify the persistence.xml file as follows:
As you can see, you also need to specify the pool alias name and the XML configuration file for Proxool.
4. Check if Proxool works
Start your application, and you should see Hibernate uses ProxoolConnectionProvider in the log:And use a database server management program like MySQL Workbench to see the list of client connections made by your application:Here, you can see there are some connection threads that are sleeping, waiting for execution. The number of connections here should equal to the minimum-connection-count property you specify in the Proxool configuration file.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments