trying to connect to database in DB2 using eclipse via mybatis returns me this error:
### 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
the database can be accessed using DB2 console and i already did set DB2COMM to TCPIP but still doesnt work
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-config
<?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
the database is named "mp" and the table is "mybatis"
full error message
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
very very late to the party but Kiran Kumar was right, this was caused by firewall issues