程序运行过程中遇到“ORA-03114: not connected to ORACLE”的问题解决

c#,winform程序,数据批量入oracle库时用到DataAdaper的.FillSchema函数,如:da.FillSchema(dt2, SchemaType.Mapped);

程序运行一段时间后在此出现中断,错误提示:ORA-03114: not connected to ORACLE。

经从网上查找相关解决方案,有说重启应用程序的;有说这是此函数本身的一个bug,需要添加什么微软官方所说的解决方案的。

经本人试验,重启应用程序是会变好。但是运行过程中还会难免出现上面的错误,这时候就还需要重启程序,所以并不是长久之计。

后经试验,只在数据库服务一停,上述问题必定会出现。也就是说只要出现过数据库连接断开的情况(比如数据库服务器断电导致网络断开,从而程序中连接数据库的地方中断),那么即使后面数据库与程序连接又可以恢复正常,那么此处还是会报ORA-03114: not connected to ORACLE的错误。说明数据库连接仍不可用。

而且经查DataAdapter.FillSchema()函数官方文档:The connection object associated with the SelectCommand must be valid,but it does not need to be open. If the connection is closed before FillSchema is called, it is opened to retrieve data and then closed. If the connection is open before FillSchema is called, it remains open.

说明此函数运行时,必须保证数据库连接可用,而且函数本身自动会打开连接,用然后再关闭。如果本身就是打开状态,那仍会保持打开状态不变。

经调试试验,改为如下方案:

try
{
da.FillSchema(dt2, SchemaType.Mapped);
}
catch (OracleException ex)
{
try{
cnnOracle = new OracleConnection(strCnnOracle);//strCnnOracle为数据库连接字符串。
cnnOracle.Open();
}
catch(OracleException ex2)
{
return  ex2.Message;
}
}

也就是说,增加了try catch,在catch中重新实例化连接,并将连接打开即可。

posted @ 2017-06-22 12:03  liuxixi  阅读(49543)  评论(0编辑  收藏  举报