【问题标题】:Unable to get JDBC connection in Spring application to MySQL无法在 Spring 应用程序中获取到 MySQL 的 JDBC 连接
【发布时间】:2014-02-17 10:21:58
【问题描述】:

我在 Spring 中有以下数据库配置:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="url" value="jdbc:dburl" />
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="username" value="username" />
            <property name="password" value="passworf" />
            <property name="removeAbandoned" value="true" />
            <property name="initialSize" value="3" />
            <property name="maxActive" value="5" />
        </bean>

用于从 DB 加载属性值的附加配置。

<bean id="config1" class="org.apache.commons.configuration.DatabaseConfiguration">
        <constructor-arg type="javax.sql.DataSource" ref="dataSource" />
        <constructor-arg index="1" value="tablename1" />
        <constructor-arg index="2" value="columnname" />
        <constructor-arg index="3" value="columnvalue" />
    </bean>

    <bean id="configFactory1"
        class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
        <constructor-arg ref="config1" />
    </bean>

    <!-- DB Properties Initialization -->
    <bean id="configurationPlaceHolder1"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="false"/>
        <property name="properties" ref="configFactory1" />
    </bean>

    <bean id="config2" class="org.apache.commons.configuration.DatabaseConfiguration">
        <constructor-arg type="javax.sql.DataSource" ref="dataSource" />
        <constructor-arg index="1" value="tablename2" />
        <constructor-arg index="2" value="columnname" />
        <constructor-arg index="3" value="columnvalue" />
    </bean>

    <bean id="configFactory2"
        class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
        <constructor-arg ref="config2" />
    </bean>

    <!-- DB Properties Initialization -->
    <bean id="configurationPlaceHolder2"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="false"/>
        <property name="properties" ref="configFactory2" />
    </bean>

DAO 配置有问题:

    <!-- DAO Bean which is causing issue -->
    <bean id="problematicBean" class="com.example.ProblematicDAOImpl" scope="singleton">
      <property name="dataSource"  ref="dataSource" />    
    </bean>

当我启动应用程序时,我卡在以下行:

    17 Feb 2014 15:39:58,981 -  main - DEBUG - 
org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:691)  - 
Executing prepared SQL query
            17 Feb 2014 15:39:58,983 -  main - DEBUG - 
org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:625)  - 
Executing prepared SQL statement [ SQL STATEMENT ]  


17 Feb 2014 15:39:59,000 -  main - DEBUG -
 org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:110)  
- Fetching JDBC Connection from DataSource

在这一行之后,应用程序挂起并且没有响应。它也不会抛出异常。

我做了以下检查:

  • 增加了maxActive 值。
  • 增加了initialSize 值。
  • 检查- SQL STATEMENT 正在Query Browser 中执行。

** 更新 **

我进一步调试了代​​码,代码挂在org.springframework.jdbc.core.JdbcTemplate中的@line 642,用于方法org.springframework.jdbc.core.JdbcTemplate.execute(PreparedStatementCreator, PreparedStatementCallback&lt;T&gt;)

代码行如下:

if (this.nativeJdbcExtractor != null) {
                psToUse = this.nativeJdbcExtractor.getNativePreparedStatement(ps);
            }
            T result = action.doInPreparedStatement(psToUse);//Hangs at this line
            handleWarnings(ps);
            return result;

【问题讨论】:

  • 第一次连接或一段时间后应用程序挂起??
  • @NiksTyagi :它在 singleton bean's init 方法中。 Bean 在启动时被初始化。我将添加更多配置以便更好地理解。
  • 我知道了।它解决了我自己。我使用 NamedParameterJdbctemplate 来调用不带任何参数的查询。这在 spring api 中执行准备好的语句时挂起。我将它切换到 JdbcTempalte 类。它工作正常。
  • @LearnMore 请为您的问题创建一个答案并接受它(或标记它以使其关闭)。 :)

标签: java mysql spring jdbc database-connection


【解决方案1】:

我知道了।它解决了我自己。我使用NamedParameterJdbctemplate 调用不带任何参数的查询。这在spring api 中执行准备好的语句时挂起。我把它切换到JdbcTempalte 类。它工作正常。

【讨论】:

    【解决方案2】:

    请检查您的 JRE 版本,因为 Microsoft jdbc 驱动程序在少数 JRE-1.6.x 版本上已损坏并导致线程挂起。这里有更多细节:- http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7105007

    【讨论】:

    • 您能否在此处添加更多详细信息,因为链接可能有一天会失效
    猜你喜欢
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 2014-07-28
    相关资源
    最近更新 更多