Connecting to Informix via DriverManager.getConnec

2019-09-07 02:50发布

问题:

I am using JDBC to connect to an Informix instance via the DriverManager.getConnection method, but I have problems.

DriverManager.getConnection takes a long time to establish a connection with Informix. I'm trying to solve this problem, but have not been successful so far.

How to solve this problem?

回答1:

You can write simple test that shows how long it takes to connect to database. For such things I like Jython that can work with JDBC, which can work with native JDBC drivers and via JDBC-ODBC bridge can work with ODBC driver. Of course you must first configure such ODBC connection.

This is test program that shows such times:

import sys
import traceback
import time

from java.sql import DriverManager
from java.lang import Class

Class.forName("com.informix.jdbc.IfxDriver")

def test_conn(db_url, usr, passwd):
    try:
        t0 = time.time()
        try:
            db = DriverManager.getConnection(db_url, usr, passwd)
            t2 = time.time()
            print('%s' % (db_url))
            print('%s, connection time %.03f [s]\n' % (db, (t2-t0)))
        finally:
            db.close()
    except:
        print("there were errors!")
        s = traceback.format_exc()
        sys.stderr.write("%s\n" % (s))


def main():
    for _ in range(5):
        test_conn('jdbc:informix-sqli://169.0.5.10:9088/test:informixserver=ol_t1;', 'user', 'passwd')
        test_conn('jdbc:odbc:ifx_test', 'user', 'passwd')

main()

Results on my machine shows that JDBC is faster in connecting then JDBC-ODBC bridge (you must know that bridge adds some time that is not necessary with native applications). Also my test is run on Windows where minimal resolution of time.time() is probably ~15 ms. My results:

jdbc:informix-sqli://169.0.5.10...
com.informix.jdbc.IfxSqliConnect@1658cfb, connection time 0.015 [s]

jdbc:odbc:test
sun.jdbc.odbc.JdbcOdbcConnection@ad75b, connection time 0.047 [s]

Of course with this program you can test the same driver but with databases on different servers.



回答2:

The problem can be that your hosts file does not contains the mapping for 127.0.0.1 localhost.

Also it could be a strange behavior of Informix using IPv6 address for port listening that I described on this other SO post: informix jdbc stuck connecting