用microsoft sql server实现hikaricp

11 java hikaricp

我试图找出使用hikaricp(JDBC连接池)与microsoft sql server的最佳方法.从我看到的,建议使用DataSource选项(就像我见过的大多数连接池一样).但是,我无法根据我看到的示例正确地与sql server数据库建立连接 - 想知道是否有人有一个可以将我的数据库信息插入的工作示例.

ali*_*der 21

确保您已采取以下步骤:

  1. 如果使用maven,请确保在pom文件中具有以下依赖关系(如果使用JDK7/8):

    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>2.0.1</version>
        <scope>compile</scope>
    </dependency>
    
    Run Code Online (Sandbox Code Playgroud)

    如果使用其他构建工具,请相应地更改资源URL(或者如果没有其他选项,只需从maven存储库下载jar文件).

    我相信你也需要你的pom文件中的sqljdbc4.jar文件(我可能错了这个要求所以我可能会在我再次确认后更新帖子)

  2. 在类中导入以下内容以及其他引用:

    import com.zaxxer.hikari.HikariConfig;
    import com.zaxxer.hikari.HikariDataSource;
    
    Run Code Online (Sandbox Code Playgroud)
  3. 添加以下最终属性(或者只是从配置文件中加载它们):

     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"; 
    
    Run Code Online (Sandbox Code Playgroud)

    您可以像这样检索连接URL:

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

    }

然后,以下内容应该为您提供所需的DataSource以获取连接:

 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;
   }
Run Code Online (Sandbox Code Playgroud)

要么

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
}
Run Code Online (Sandbox Code Playgroud)

首选路由是将HikariConfig传递给HikariDataSource构造函数.您还可以从属性文件加载配置.

然后从数据源获取连接:

Connection con = null;
con = ds.getConnection();  //where ds is the dataSource retrieved from step 5
Run Code Online (Sandbox Code Playgroud)

  • 谢谢 - 事实证明我在使用hikaricip进行测试时使用了错误的DataSourceClassName - 我喜欢这样一个事实:有很多人拥有亲密的选票但只有一个答案 - 关于这些人的更多信息比关于我的问题更多 (7认同)