Question

I have a web application that uses MySQL and HikariCP for the connection pooling. I connect to my pool using a singleton connection pool object defined like so:

package com.webapp.db;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.log4j.Logger;

public class HikariConnectionPool
{
    private static final Logger log = Logger.getLogger(HikariConnectionPool.class.getName());
    private static volatile HikariConnectionPool hikariCP = null;
    private static HikariDataSource hikariDataSource = null;

    private HikariConnectionPool() // We don't want any other object creating this pool
    {  
        HikariConfig config = new HikariConfig();
        config.addDataSourceProperty("url","jdbc:mysql://remoteMYSQLServer:3306/webapp");
        config.addDataSourceProperty("user", "webapp");
        config.addDataSourceProperty("password", "password");
        config.setMinimumIdle(1);
        config.setMaximumPoolSize(2);
        config.setInitializationFailFast(true);
        config.setConnectionTestQuery("VALUES 1");
        config.setJdbcUrl("jdbc:mysql://remoteMYSQLServer:3306/webapp");
        config.setDriverClassName ("com.mysql.jdbc.Driver");

         hikariDataSource = new HikariDataSource (config);
   }

   public static HikariConnectionPool getInstance()
   {
    if (hikariCP == null)
        {
        synchronized (HikariConnectionPool.class)
            {
            if (hikariCP == null)
                {
                hikariCP = new HikariConnectionPool ();
                }
            }
        }
    return hikariCP;
    }

    public static HikariDataSource getDataSource ()
    {
    hikariCP = getInstance ();
    return hikariDataSource;
    }
}

In my application, I use the following code to get a data source:

HikariDataSource ds = HikariConnectionPool.getDataSource ();

and then try to insert into the database using

try {
Connection connection = ds.getConnection ();
String sString = "insert into webapp.sometable (?, ?, ?, ?, ?);";
PreparedStatement statement = connection.prepareStatement (sString);
statement.setString (1, fname);
statement.setString (2, sname);
statement.setString (3, email);
statement.setString (4, password);
statement.setString (5, phone);

statement.executeUpdate();
connection.commit ();
connection.close ();
} catch (SQLException e) { e.printStackTrace(); }

but I do not see the values committed to the database. What am I doing wrong? Any help is deeply appreciated.

Was it helpful?

Solution

Your getInstance() with the double-checked locking is incorrect and could cause a race condition. See "Double-checked locking" on Wikipedia for a correct pattern.

Your SQL statement does not need to end with a semicolon. And it probably does not need to be qualified with the schema "webapp." because you are explicitly connecting to the webapp database.

Also, I'd skip the Driver Manager version of constructing a datasource and go with the following pattern:

HikariConfig config = new HikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(2);
config.setInitializationFailFast(true);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
config.addDataSourceProperty("serverName", "localhost");
config.addDataSourceProperty("port", "3306");
config.addDataSourceProperty("databaseName", "webapp");
config.addDataSourceProperty("user", "webapp");
config.addDataSourceProperty("password", "password");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top