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

ε祈祈猫儿з 提交于 2019-12-06 00:57:14

问题


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.


回答1:


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).




回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/35250636/configuration-of-jtds-for-use-with-hikaricp-spring-ms-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!