IBM的文档是很详细,并且很好查的。

以下是我google时用的关键字

DB2 SQLCODE=-1391

前三条结果是其他外国程序员在各种社区问的问题,不用看直接跳过,第一页一定有IBM官方文档。

果然,第四条就是:IBM Knowledge Centerwww.ibm.com

然后ctrl+f 输入1391

SQL1391N The database is already in use by another instance of the database manager.

Explanation

The request failed because the database is in use by another instance of the database manager (a database may only be used by one instance.) This may be caused by attempting to access a database located on a mounted file system accessible to another instance located on another machine.

This can also occur if you have an open connection (via SNA) to a database and the database manager was brought down abnormally.

User response

Validate that you are using the correct database and ensure no other instance uses this database.

If the database manager was brought down abnormally, and you have a command line processor connection to it, perform a db2 terminate to close the offending open connection before attempting to connect again.

sqlcode: -1391

sqlstate: 42703

错误码对应的原因、详细解释、用户反馈都在这里了。

IBM的官方文档真的非常棒,Read it pls.

后续解答,刚跟小姐姐吃完火锅消消食

问题(我修改了一下):

Q1: 如果数据库没有任何连接的时候java是可以连接成功的 而java连接成功之后执行db2 connect to 同名库却不能成功,why(我也确实查到过实例被其他实例占用的报错信息,但是他们两个连接的是同一个实例下的库,如果java不连数据库其他连接都是正常的)?

我觉得非常有可能你连上之后就没有释放过连接,因为你测试代码就没写释放,这估计是你的编码习惯。

你可以手动连上db2的服务器,查一下当前hold住那个SQLINSLK文件的进程是啥,我估计是你的java项目。 怎么查这个进程我也不记得了,命令自己找一下吧。

其实括号里面你说的内容代表你没看懂那个文档,无论是你java里面用JDBC去连,还是你用navicat这类管理工具去连,还是你直接用db2 connect去练,本质都是一样。

这个所谓的本质就是文档里的 instance of the database manager 。

也就是说,当你的java代码里的 conn=(Connection)DriverManager.getConnection(url,user,pwd); ,这个conn对象(他就是一个instance of the database manager)拿到连接之后再也没有释放过,所以你db2 connect(相当于另外一个instance of the database manager )要去连肯定不给你连。

然后你那个 “如果java不连数据库其他连接都是正常的”也印证了这一点,毕竟其他工具谁敢拿起来连接就打死不释放了。。。

Q2 java用jcc连接的时候读取的不是当前使用的实例而是从某个配置文件读取的?

你遇到的这个情况,跟配置文件一毛钱关系都没有。 因为无论缺少 url port name pwd 还是dbname 都会抛出对应的异常的,不要瞧不起IBM写DB2Driver的老哥。

我念书的时候老师不许我们用框架,也不许我们用JDBC,让我们手动用C .net Java 三种语言手动撸自己的连接数据库的Driver,而且要撸至少三种关系型数据库,SQL server/DB2(Oracle 二选一)/MySql ...

当年我是完全不理解的,现在懂了,老师就是觉得我们这群菜鸡又菜又浮躁,每天不好好研究基础,净整点花里胡哨的。

最后我为了对(闲)你(的)负(蛋)责(疼),我去拉了我能找到的最新版本的db2jcc,然后大致扫了一眼包里面的东西,还是那个熟悉的味道...

所以我建议你加入类似下方的代码

finally {
conn.close();
}

然后你再DB2 connect 一下试试,这问题估计就解决了。

当然,你别真的所有代码都这么释放连接,那就太蠢了,这个是让你验证答案用的。