日期:2014-05-20  浏览次数:20853 次

c++调用java,然后访问oracle出现的问题
在一个工程中,使用了jdbc加上oracle的jdbc驱动完成了访问oracle(8或者9)数据库的部分,由于需要保露c++、c的接口所以业务逻辑采用从c++编写。
在经历了一番摸索后,还是有一个很苦恼的问题的困扰我,还是先把代码粘上来,
1.5 JDK
java:

public int ChangeConnection(String IPString, String PortString, String DBName, String UserName, String Pwd)
{
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
thin_con = DriverManager.getConnection(connectionString, UserName, Pwd);
///OUT
stmt = thin_con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rel_stmt = thin_con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
list_stmt = thin_con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
upDate_stmt = thin_con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
connectionString = recorder;

return 1;
} catch (Exception e) {
err = e.toString();
System.out.println(err);
return -1;
}
}

c++:
获取jni,或者attach一个jni,然后调用以上的方法。

出现的问题就是,在vc中完全编译后,启动会成功过去,但是去bin目录下面,启动生成的exe文件就会有可能出现访问冲突的错误,直接退出程序,而且有时候是直接在bin目录中启动exe就是正常的,启动几次后会出现同样的错误,但是只要在vc中启动,只要第一次是正确的,就永远是正确的,无论多少次,而且,当bin/exe启动失败后,vc的也将永远的启动失败,即使是debug模式也错误,只有重新的完全编译,而且完全编译后的bin/exe也不能保证会正确,或者永远正确,有个幸运的版本可以永远正确,可是当拷贝给同事的时候又出现同样的错误了。
后来验证是在java代码中的OUT前的那条语句出错的。之间都是好好的。
具体的错误如下(有点长),敬请专家指教:
#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d6c8b19, pid=1188, tid=848
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0_01-b08 mixed mode, sharing)
# Problematic frame:
# V [jvm.dll+0x88b19]
#

--------------- T H R E A D ---------------

Current thread (0x00a13580): JavaThread "main" [_thread_in_vm, id=848]

siginfo: ExceptionCode=0xc0000005, reading address 0x0099f000

Registers:
EAX=0x00000006, EBX=0x0012f630, ECX=0x0099f000, EDX=0x2b416138
ESP=0x0012f368, EBP=0x0012f37c, ESI=0x00000052, EDI=0x00a13580
EIP=0x6d6c8b19, EFLAGS=0x00010246

Top of Stack: (sp=0x0012f368)
0x0012f368: 0000048c 00a1363c 0099f000 0012f384
0x0012f378: 0012f384 0012f5e0 6d4c5973 00a13580
0x0012f388: 0099f000 00000052 0012f630 00a13580
0x0012f398: 2ad4cfe0 2b641e50 0012f618 0012f538
0x0012f3a8: 00a13580 00a13140 00a14c00 00a14c3c
0x0012f3b8: 00a14ff4 0012fc20 0012f540 01457510
0x0012f3c8: 0012f40c 6d71481d 0000000b 00000000
0x0012f3d8: 0012f474 00000000 00a14c20 00a13580 

Instructions: (pc=0x6d6c8b19)
0x6d6c8b09: 7d 08 e8 96 7a fb ff 8b 4d 0c 8b 75 10 8b 5d 14
0x6d6c8b19: 8b 01 c1 ee 02 83 3d d8 69 7a 6d 00 8b 50 04 74 


Stack: [0x00030000,0x00130000), sp=0x0012f368, free space=1020k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
V [jvm.dll+0x88b19]
C [net.dll+0x5973]
j java.net.PlainSocketImpl.socketConnect(Ljava/net/InetAddress;II)V+0
j java.net.PlainSocketImpl.doConnect(Ljava/net/InetAddress;II)V+10
j java.net.PlainSocketImpl.connectToAddress(Ljava/net/InetAddress;II)V+23
j java.net.PlainSocketImpl.connect(Ljava/net/SocketAddress;I)V+71
j java.net.SocksSocketImpl.connect(Ljava/net/SocketAddress;I)V+332
j java.net.Socket.connect(Ljava/net/SocketAddress;I)V+165
j java.net.Socket.connect(Ljava/net/SocketAddress;)V+3
j java.net.Socket.<init>(Ljava/net/SocketAddress;Ljava/net/SocketAddress;Z)V+95
j java.net.Socket.<init>(Ljava/lang/String;I)V+38
j oracle.net.nt.TcpNTAdapter.connect()V+13
j oracle.net.nt.ConnOption.connect()V+12
j oracle.net.nt.ConnStrategy.execute()Loracle/net/nt/ConnOption;+24
j oracle.net.resolver.AddrResolution.resolveAndExecute(Ljava/lang/String;)Loracle/net/nt/ConnOption;+91
j oracle.net.ns.NSProtocol.establishConnection(Ljava/lang/String;)Loracle/net/ns/SessionAtts;+9