14

I am trying to figure out the best approach for using hikaricp (JDBC connection pool) with microsoft sql server. From what I saw, the DataSource option is recommended (as is the case for most connection pools I've seen). However, I was not able to form a connection correctly with the sql server database based on the examples I've seen - wondering if anyone has a working example to which I can plug my DB info into.

1 Answers1

27

Make sure you have taken the following steps:

  1. If using maven, make sure that you have the following dependency in your pom file (if using JDK7/8):

    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>2.0.1</version>
        <scope>compile</scope>
    </dependency>
    

    If using another build tool, change the resource URL accordingly (or just download the jar file from the maven repository if there is no other option for you).

    I believe you need the sqljdbc4.jar file in your pom file as well (I could be wrong about this requirement so I may update the post once I reconfirm)

  2. Import the following in your class along with other references:

    import com.zaxxer.hikari.HikariConfig;
    import com.zaxxer.hikari.HikariDataSource;
    
  3. Add the following final properties (or simply load them from config file):

     private final String url = "jdbc:sqlserver://";
     private final String serverName= "xxx.xxx.xxx.xxx";   
     private final int portNumber = 1433;
     private final String databaseName= "ACTUALDBNAME";    
    
     private final String userName = "ACTUALUSERNAME"; 
     private final String password = "ACTUALPASSWORD";
    
     private final String selectMethod = "cursor"; 
    

    You can retrieve the connection URL like this:

     public String getConnectionUrl() {
          return url+this.serverName+":"+this.portNumber+";databaseName="+this.databaseName+";user="+this.userName+";password="+this.password+";selectMethod="+this.selectMethod+";";
    

    }

Then, the following should give you the DataSource you need in order to get a connection:

 public DataSource getDataSource() {
      final HikariDataSource ds = new HikariDataSource(); 
      ds.setMaximumPoolSize(10);
      ds.setDataSourceClassName("com.microsoft.sqlserver.jdbc.SQLServerDataSource");
     // ds.addDataSourceProperty("serverName", this.serverName);
     //ds.addDataSourceProperty("databaseName", this.databaseName);
      ds.addDataSourceProperty("url", this.getConnectionUrl());
      ds.addDataSourceProperty("user", this.userName);
      ds.addDataSourceProperty("password", this.password);
      ds.setInitializationFailFast(true);
      ds.setPoolName("wmHikariCp");
      return ds;
   }

or

public DataSource getDataSource() {
     HikariConfig config = new HikariConfig();
     config.setMaximumPoolSize(10);
     config.setDataSourceClassName("com.microsoft.sqlserver.jdbc.SQLServerDataSource");
     config.addDataSourceProperty("serverName", this.serverName);
     config.addDataSourceProperty("port", this.portNumber);
     config.addDataSourceProperty("databaseName", this.databaseName);
     config.addDataSourceProperty("user", this.userName);
     config.addDataSourceProperty("password", this.password);

     return new HikariDataSource(config);  //pass in HikariConfig to HikariDataSource
}

The preferred route is to pass the HikariConfig to the HikariDataSource constructor. You can also load the config from a properties file.

Then get connection from the datasource:

Connection con = null;
con = ds.getConnection();  //where ds is the dataSource retrieved from step 5
Car4p17
  • 45
  • 1
  • 6
ali haider
  • 19,175
  • 17
  • 80
  • 149
  • 7
    thanks - it turned out I was using the wrong DataSourceClassName when testing with hikaricip - I love the fact that there are plenty of people with close votes yet only one answer - says more about these folks than it does about my question –  Aug 18 '14 at 15:44