【问题标题】:MyBatis-Spring: SqlSessionFactoryBean initialization: connection timeoutMyBatis-Spring:SqlSessionFactoryBean 初始化:连接超时
【发布时间】:2013-08-09 19:57:04
【问题描述】:

我正在 Spring 3.0 环境中从 iBatis 2 迁移到 MyBatis 3。当我尝试部署应用程序,甚至只是运行 JUnit 测试时,配置初始化失败并出现如下连接超时错误:

在类路径资源 [testContext-jndi.xml] 中创建名称为“sqlSessionFactory”的 bean 时出错:调用 init 方法失败;嵌套异常是 org.apache.ibatis.builder.BuilderException:创建文档实例时出错。原因:java.net.ConnectException:连接超时:连接

它看起来像是缺少 http 代理,但我的 eclipse 代理设置一直适用于我的所有其他代码。我的 DTD 定义似乎是正确的。

我使用的是 Spring 3.0.0,并在我的类路径中包含以下 jar:

  • mybatis-3.2.2.jar
  • mybatis-spring-1.2.0.jar

相关spring配置如下:

fooContext-data.xml - (这是测试/资源配置。部署到 Web 容器时,spring 配置的数据源使用 JNDI 查找来获取凭据。它们都以相同的方式失败。)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <bean id="dataSourceFoo" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
        <property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@(DESCRIPTION = (LOAD_BALANCE=on)(FAILOVER=on)(ADDRESS=(PROTOCOL=tcp)(HOST=foo1)(PORT=1521))(ADDRESS=(PROTOCOL=tcp)(HOST=foo2)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=foo.bar.boz)))"/>
        <property name="username" value="fooUser"/>
        <property name="password" value="fooPass"/>             
    </bean>

    <!-- MyBatis stuff -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSourceFoo" />
        <property name="configLocation" value="sqlMapConfig.xml" />
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>


</beans>

sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//www.mybatis.org//DTD Config 3.0//EN"
        "http://www.mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

    <properties resource="ibatis.properties" />

    <mappers>
        <mapper resource="foo/dao/maps/mybatis/SqlMap1.xml"/>
        <mapper resource="foo/dao/maps/mybatis/SqlMap2.xml"/>
        <mapper resource="foo/dao/maps/mybatis/SqlMap3.xml"/>
        <mapper resource="foo/dao/maps/mybatis/SqlMap4.xml"/>
        <mapper resource="foo/dao/maps/mybatis/SqlMap5.xml"/>
        <mapper resource="foo/dao/maps/mybatis/SqlMap6.xml"/>
        <mapper resource="foo/dao/maps/mybatis/SqlMap7.xml"/>
        <mapper resource="foo/dao/maps/mybatis/SqlMap8.xml"/>
        <mapper resource="foo/dao/maps/mybatis/SqlMap9.xml"/>
        <mapper resource="foo/dao/maps/mybatis/SqlMap10.xml"/>
        <mapper resource="foo/dao/maps/mybatis/SqlMap11.xml"/>
        <mapper resource="foo/dao/maps/mybatis/SqlMap12.xml"/>
        <mapper resource="foo/dao/maps/mybatis/SqlMap13.xml"/>
        <mapper resource="foo/dao/maps/mybatis/SqlMap14.xml"/>
        <mapper resource="foo/dao/maps/mybatis/SqlMap15.xml"/>
    </mappers>

</configuration>

我注意到其他一些人遇到了这个问题,这与 myBatis xml 的 DTD 有问题有关,或者网络连接不好,导致他们无法下拉 DTD 定义。我看不出我怎么会遇到这个问题,因为我的场景似乎涵盖了所有这些基础。

我已经解决这个问题一天半了,我翻遍了 MyBatis 和 MyBatis-Spring 的文档,以及这个网站和其他网站的很多内容。任何想法将不胜感激。

【问题讨论】:

    标签: spring configuration mybatis dtd xml


    【解决方案1】:

    我在 myBatis 用户网站上找到了这个问题的答案: DTD verification when www.mybatis.org is down

    原来我的 DTD 链接不应该在它们前面有 www。也就是说,而不是:

        <!DOCTYPE configuration
            PUBLIC "-//www.mybatis.org//DTD Config 3.0//EN"
            "http://www.mybatis.org/dtd/mybatis-3-config.dtd">
    

    他们应该阅读:

        <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    

    我改变了这些,现在它可以工作了。

    【讨论】:

      猜你喜欢
      • 2013-09-07
      • 1970-01-01
      • 2013-11-29
      • 2017-12-29
      • 1970-01-01
      • 2022-08-14
      • 1970-01-01
      • 2018-04-25
      • 1970-01-01
      相关资源
      最近更新 更多