curious1
curious1

Reputation: 14727

Configuration of JTDS for use with HikariCP + Spring + MS SQL Server

I kept googling for configuration of JTDS (1.3.1) for use with HikariCP (2.4.3), Spring (4.1.2), and MS SQL Server (2008), but unable to find a complete and working example.

Here is what I have:

<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
    <constructor-arg ref="hikariConfig" />
</bean> 

<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
    <property name="poolName" value="springHikariCP" />
    <property name="connectionTestQuery" value="SELECT 1" />
    <property name="dataSourceClassName" value="${jdbc.dataSourceClassName}" />
    <property name="maximumPoolSize" value="${jdbc.maximumPoolSize}" />
    <property name="minimumIdle" value="${jdbc.minimumIdle}" />
    <property name="idleTimeout" value="${jdbc.idleTimeout}" />
    ....
    <property name="dataSourceProperties">
        <props>
            ....
        </props>
    </property>
</bean>

Can anyone out there share the JTDS configs used in a production environment?

Regards.

UPDATE

I found this SO post:

HikariCP hanging on getConnection

It seems that JTDS has a problem working with HikariCP. Actually, I have this problem too. Here is my complete config for JTDS:

<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
    <constructor-arg ref="hikariConfig" />
</bean> 

<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
    <property name="poolName" value="springHikariCP" />
    <property name="connectionTestQuery" value="${jdbc.connectionTestQuery}" />
    <property name="dataSourceClassName" value="${jdbc.dataSourceClassName}" />
    <property name="maximumPoolSize" value="${jdbc.maximumPoolSize}" />
    <property name="minimumIdle" value="${jdbc.minimumIdle}" />
    <property name="idleTimeout" value="${jdbc.idleTimeout}" />
    <property name="connectionTimeout" value="${jdbc.connectionTimeout}" />
    <property name="jdbcUrl" value="${jdbc.url}" />
    <property name="dataSourceProperties">
        <props>
            <prop key="user">${jdbc.username}</prop>
            <prop key="password">${jdbc.password}</prop>
            <prop key="cacheMetaData">${jtds.cacheMetaData}</prop>                              
        </props>
    </property>
</bean>

This is exactly the reason I posted my question, and I hope to see a complete example. However, at HikariCP's page, JTDS is listed as supported. I am confused.

Upvotes: 6

Views: 8177

Answers (4)

Raystorm
Raystorm

Reputation: 6548

Just adding a Validation Query (ConnectionTestQuery) to my config didn't work for me.
I ended up having to Wrap my JtdsDataSource in a HikariDataSource to get it to work.

In a @Configuration class

   public DataSource getDataSource()
   {
      //initial JtdsDataSource to control the DB connection
      JtdsDataSource ds = new JtdsDataSource();

      ds.setDomain(this.domain);
      ds.setServerName(this.server);
      ds.setDatabaseName(this.database);
      ds.setUser(this.user);
      ds.setPassword(this.password);

      //settings for integrated authentication to work.
      ds.setSsl(SSL_REQUEST); //static import from net.sourceforge.jtds.ssl.Ssl
      ds.setUseNTLMV2(true);

      //WRAP JTDS in a Hikari Datsource
      HikariConfig hc = new HikariConfig();
      hc.setDataSource(ds);
      hc.setConnectionTestQuery("SELECT 1");

      HikariDataSource hds = new HikariDataSource(hc);
      return hds;
   }

Upvotes: 0

Abhinav Mishra
Abhinav Mishra

Reputation: 161

Both of these approaches worked for me. I am able to use db connection properly.

<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">  
      <property name="poolName" value="tmmConnPool"/>
      <property name="connectionTestQuery" value="SELECT 1"/>    
      <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
      <property name="maximumPoolSize" value="10"/>
      <property name="jdbcUrl" value="jdbc:jtds:sqlserver://127.0.0.1:1433/users;domain=workgroup"/>
      <property name="username" value="sa"/>
      <property name="password" value="admin"/>
    </bean>

    <!-- OR -->

     <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">  
      <property name="poolName" value="tmmConnPool"/>
      <property name="connectionTestQuery" value="SELECT 1"/>    
      <property name="dataSourceClassName" value="net.sourceforge.jtds.jdbcx.JtdsDataSource"/>
      <property name="maximumPoolSize" value="10"/>
      <property name="dataSourceProperties">
         <props>
           <prop key="user">sa</prop>
           <prop key="password">admin</prop>
           <prop key="serverName">127.0.0.1</prop>    
           <prop key="portNumber">1433</prop>    
           <prop key="databaseName">users</prop>    
           <!-- For SQLServer value is 1 -->   
           <prop key="serverType">1</prop>   
           <prop key="domain">workgroup</prop>                    
          </props>
      </property>
    </bean>  

    <!-- Reference of above databSource, both options given above works -->

    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">  
       <constructor-arg ref="hikariConfig"/>  
    </bean>  

Apart from this, i was able to get connection using java implementation as well

public Connection getConnectionFromPool() {
        final HikariConfig hikariCfg = new HikariConfig();
        hikariCfg.setPoolName("tmmConnectionPool");
        hikariCfg.setDataSourceClassName("net.sourceforge.jtds.jdbcx.JtdsDataSource");
        hikariCfg.setConnectionTestQuery("SELECT 1");
        hikariCfg.setMaximumPoolSize(10);
        hikariCfg.getDataSourceProperties().put("user", "sa);
        hikariCfg.getDataSourceProperties().put("password", "admin");
        hikariCfg.getDataSourceProperties().put("serverName", "127.0.0.1");
        hikariCfg.getDataSourceProperties().put("portNumber", "1433");
        hikariCfg.getDataSourceProperties().put("databaseName", "users");
        hikariCfg.getDataSourceProperties().put("serverType", "1");
        hikariCfg.getDataSourceProperties().put("domain", "workgroup");


        final HikariDataSource hikariDs = new HikariDataSource(hikariCfg);
        Connection conn = null;
        try {
            conn = hikariDs.getConnection();
        } catch (SQLException excp) {
            LOGGER.error("Exception occurred while getting connection from dataSource", excp);
        } finally {
            if (hikariDs !=null) {
                hikariDs.close();
            }
        }
        return conn;
    }

Upvotes: -1

Federico Traiman
Federico Traiman

Reputation: 1251

Based on @roded I implemented it on this way and it worked for me.

    @Bean
    public JdbcTemplate jdbcTemplate() {
        HikariConfig c = new HikariConfig();
        c.setDriverClassName("net.sourceforge.jtds.jdbc.Driver");
        c.setConnectionTestQuery("SELECT GETDATE()");
        c.setMaximumPoolSize(32);
        c.setJdbcUrl("jdbc:jtds:sqlserver://serverIp:1433/DBName;");
        c.getDataSourceProperties().put("user", "user");
        c.getDataSourceProperties().put("password", "password");
        c.getDataSourceProperties().put("cacheMetaData", true);

        HikariDataSource hds = new HikariDataSource(c);

        JdbcTemplate jdbcTemplate = new JdbcTemplate(hds);

    return jdbcTemplate;
}

Upvotes: 2

roded
roded

Reputation: 466

The following works:

<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close" depends-on="flyway">
    <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
    <property name="connectionTestQuery" value="SELECT GETDATE()"/>
    <property name="maximumPoolSize" value="32"/>
    <property name="jdbcUrl" value="${dbUrl}"/>
    <property name="username" value="${dbUsername}"/>
    <property name="password" value="${dbPassword}"/>
</bean>

Notice the connectionTestQuery property which is required so Hikari will not assume the driver is JDBC4 compliant (jTDS is 3.0 compliant).

Upvotes: 6

Related Questions