DB2和MyBatis Connection被拒绝:连接。 ERRORCODE = -4499,SQLSTATE = 08001

时间:2016-12-19 09:08:23

标签: java db2 mybatis

尝试使用eclipse通过mybatis连接到DB2中的数据库时返回错误:

### Error opening session.  Cause: com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2043][11550][4.19.49] Exception java.net.ConnectException: Error opening socket to server localhost/127.0.0.1 on port 50,000 with message: Connection refused: connect. ERRORCODE=-4499, SQLSTATE=08001

可以使用DB2控制台访问数据库,我已经将DB2COMM设置为TCPIP但仍然无法正常工作

test.java

public class Test {
    public static void main(String[] args){
        Reader reader = null;
        SqlSessionFactory sqlSessionFactory = null;
        SqlSession session = null;

        try {
            reader = Resources.getResourceAsReader("mybatis-config.xml");
        } catch (IOException e) {
            System.out.println("MyBatis config not found!");
            e.printStackTrace();
        }

        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        session = sqlSessionFactory.openSession();

        User user = new User();
        user.setName("second");
        user.setNumber(2);
        session.insert("User.addNew", user);

        int num = session.selectOne("User.getNumber", "second");
        System.out.println(num);

        session.close();
    }
}

mapper.xml

<mapper namespace="User">
    <resultMap type="int" id="resultNumber">
        <result property="number" column="NUMBER" />
    </resultMap>
    <resultMap type="User" id="resultUser">
        <result property="name" column="NAME" />
        <result property="number" column="NUMBER" />
    </resultMap>
    <select id="getNumber" parameterType="String" resultMap="resultNumber">
        SELECT number
        FROM mp.mybatis
        WHERE name = #{name}
    </select>
    <select id="getUser" parameterType="String" resultMap="resultUser">
        SELECT *
        FROM mp.mybatis
        WHERE name = #{name}
    </select>
    <insert id="addNew" parameterType="User">
        INSERT INTO mp.mybatis (name, number)
        VALUES (#{name}, #{number})
    </insert>
    <update id="updateUser" parameterType="map">
        UPDATE mp.mybatis
        SET name = #{new.name}, number = #{new.number}
        WHERE name = #{old.name} AND number = #{old.number}
    </update>
    <delete id="deleteUser" parameterType="String">
        DELETE FROM mp.mybatis
        WHERE name = #{name}
    </delete>
</mapper>

的MyBatis-配置

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE configuration
  PUBLIC '-//mybatis.org//DTD Config 3.0//EN'
  'http://mybatis.org/dtd/mybatis-3-config.dtd'>
<configuration>
    <properties resource='jdbc.properties' />
    <typeAliases>
        <typeAlias type='com.MyBatisMP.POJO.User' alias='User'></typeAlias>
    </typeAliases>
    <environments default='development'>
        <environment id='development'>
            <transactionManager type='JDBC' />
            <dataSource type='POOLED'>
                <property name='driver' value='${jdbc.driverClassName}' />
                <property name='url' value='${jdbc.url}' />
                <property name='username' value='${jdbc.username}' />
                <property name='password' value='${jdbc.password}' />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource='com/MyBatisMP/Mapper/UserMapper.xml' />
    </mappers>
</configuration>

jdbc.properties

jdbc.driverClassName=com.ibm.db2.jcc.DB2Driver
jdbc.url=jdbc:db2://localhost:50000/mp
jdbc.username=XXX
jdbc.password=XXX

数据库被命名为&#34; mp&#34;这张桌子是&#34; mybatis&#34;

完整的错误消息

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error opening session.  Cause: com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2043][11550][4.19.49] Exception java.net.ConnectException: Error opening socket to server localhost/127.0.0.1 on port 50,000 with message: Connection refused: connect. ERRORCODE=-4499, SQLSTATE=08001
### Cause: com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2043][11550][4.19.49] Exception java.net.ConnectException: Error opening socket to server localhost/127.0.0.1 on port 50,000 with message: Connection refused: connect. ERRORCODE=-4499, SQLSTATE=08001
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)
    at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:81)
    at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:32)
    at com.MyBatisMP.Test.Test.main(Test.java:27)
Caused by: com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2043][11550][4.19.49] Exception java.net.ConnectException: Error opening socket to server localhost/127.0.0.1 on port 50,000 with message: Connection refused: connect. ERRORCODE=-4499, SQLSTATE=08001
    at com.ibm.db2.jcc.am.kd.a(Unknown Source)
    at com.ibm.db2.jcc.am.kd.a(Unknown Source)
    at com.ibm.db2.jcc.t4.ac.a(Unknown Source)
    at com.ibm.db2.jcc.t4.ac.<init>(Unknown Source)
    at com.ibm.db2.jcc.t4.a.b(Unknown Source)
    at com.ibm.db2.jcc.t4.b.newAgent_(Unknown Source)
    at com.ibm.db2.jcc.am.Connection.initConnection(Unknown Source)
    at com.ibm.db2.jcc.am.Connection.<init>(Unknown Source)
    at com.ibm.db2.jcc.t4.b.<init>(Unknown Source)
    at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(Unknown Source)
    at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(Unknown Source)
    at com.ibm.db2.jcc.DB2Driver.connect(Unknown Source)
    at com.ibm.db2.jcc.DB2Driver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.getConnection(UnpooledDataSource.java:64)
    at org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:349)
    at org.apache.ibatis.datasource.pooled.PooledDataSource.getConnection(PooledDataSource.java:55)
    at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:72)
    ... 2 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.ibm.db2.jcc.t4.w.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    ... 19 more

1 个答案:

答案 0 :(得分:1)

参加聚会的时间很晚,但是Kiran Kumar是正确的,这是由防火墙问题引起的