java - implementing hikaricp with microsoft sql server -


i trying figure out best approach using hikaricp (jdbc connection pool) microsoft sql server. saw, datasource option recommended (as case connection pools i've seen). however, not able form connection correctly sql server database based on examples i've seen - wondering if has working example can plug db info into.

make sure have take following steps:

if using maven, make sure have following dependency in 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 build tool, change resource url accordingly (or download jar file maven repository if there no other option you).

i believe need sqljdbc4.jar file in pom file (i wrong requirement may update post once reconfirm)

import following in class along other references:

    import com.zaxxer.hikari.hikariconfig;     import com.zaxxer.hikari.hikaridatasource; 

add following final properties (or load them 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 connection url this:

public string getconnectionurl() {      return url+this.servername+":"+this.portnumber+";databasename="+this.databasename+";user="+this.username+";password="+this.password+";selectmethod="+this.selectmethod+";"; } 

the following should give datasource need (to connection from):

 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 hikaridatasource } 

the preferred route pass hikariconfig hikaridatasource constructor. can load config properties file.

then connection datasource:

connection con = null; con = ds.getconnection();  //where ds datasource retrieved step 5 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -