用Microsoft SQL Server实现Hikaricp [英] implementing hikaricp with microsoft sql server
问题描述
我正在尝试找出在Microsoft SQL Server中使用hikaricp(JDBC连接池)的最佳方法.从我所见,建议使用DataSource选项(就像我所见过的大多数连接池一样).但是,根据我所看到的示例,我无法与sql server数据库正确建立连接-想知道是否有人可以将我的数据库信息插入到该示例中.
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.
推荐答案
确保已执行以下步骤:
-
如果使用Maven,请确保您的pom文件具有以下依赖性(如果使用JDK7/8):
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>
如果使用其他构建工具,请相应地更改资源URL(或者,如果没有其他选择,则可以从maven存储库下载jar文件).
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).
我相信您的pom文件中也需要sqljdbc4.jar文件(我可能对此要求有误,因此一旦我再次确认,我可能会更新该帖子)
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)
将以下内容与其他参考文献一起导入您的课堂:
Import the following in your class along with other references:
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
添加以下最终属性(或直接从配置文件中加载它们):
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";
您可以像这样检索连接URL:
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;
}
或
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
}
首选方法是将HikariConfig传递给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
这篇关于用Microsoft SQL Server实现Hikaricp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!