PrepareStatement/Commit sequence not working in mysql/HikariCP webapp

南笙酒味 提交于 2019-12-23 18:13:42

问题


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.


回答1:


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");


来源:https://stackoverflow.com/questions/22738525/preparestatement-commit-sequence-not-working-in-mysql-hikaricp-webapp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!