用于HikariCP + Spring + MS SQL Server的JTDS配置

时间:2016-02-07 06:50:35

标签: spring jtds hikaricp

我一直在谷歌搜索配置JTDS(1.3.1)以便与HikariCP(2.4.3),Spring(4.1.2)和MS SQL Server(2008)一起使用,但无法找到完整且有效的示例。

这就是我所拥有的:

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

那里的任何人都可以共享生产环境中使用的JTDS配置吗?

问候。

更新

我发现了这个帖子:

HikariCP hanging on getConnection

似乎JTDS在使用HikariCP时遇到了问题。实际上,我也有这个问题。这是我对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>

这正是我发布问题的原因,我希望看到一个完整的例子。但是,在HikariCP的页面上,JTDS被列为支持。我很困惑。

3 个答案:

答案 0 :(得分:3)

以下作品:

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

请注意所需的connectionTestQuery属性,以便Hikari不会认为驱动程序符合JDBC4(jTDS符合3.0)。

答案 1 :(得分:1)

基于@roded,我以这种方式实现了它,它对我有用。

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

答案 2 :(得分:-1)

这两种方法都适用于我。我能够正确使用数据库连接。

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

除此之外,我还能够使用java实现连接

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