You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ORA-03114:未连接至ORACLE异常——C#连接Oracle取数故障排查

Troubleshooting ORA-03114: Not Connected to ORACLE in Your C# Oracle Connection Code

Hey there, let's work through this ORA-03114 error you're hitting. This error usually means your application lost its connection to the Oracle database before executing your PL/SQL block—so let's break down the most likely fixes step by step:

1. Validate Connection Pool Health

It looks like you're pulling connections from a pool via GetConnFromPool(). Over time, connections in the pool can go stale (e.g., if the database restarted, or the connection timed out due to inactivity).

  • Add a quick validation step right after opening the connection to confirm it's still alive. For Oracle, run a simple test query:
    dbConn.Open();
    // Validate the connection first
    var testCmd = dbConn.CreateDbCommand("SELECT 1 FROM DUAL", CommandType.Text);
    testCmd.ExecuteScalar(); // If this throws, the connection is invalid
    
  • If you're using Oracle's official ODP.NET driver, you can also use the built-in Ping() method:
    var oracleConn = dbConn as OracleConnection;
    if (oracleConn != null && !oracleConn.Ping())
    {
        throw new InvalidOperationException("Connection to Oracle is no longer active.");
    }
    

2. Fix Connection String Settings

Tweak your connection string to prevent stale connections from being reused:

  • Add Validate Connection=True: This tells the connection pool to check if a connection is alive before returning it to your app. Example snippet:
    Data Source=YourOracleDB;User Id=YourUser;Password=YourPass;Validate Connection=True;
    
  • Double-check all connection details: Verify the TNS alias, host/port, SID/service name, and credentials are correct—even a small typo can silently break connections.

3. Ensure Proper Connection Lifecycle Management

Always wrap connections in using statements to guarantee they're correctly released back to the pool (and avoid leaks that lead to stale connections):

using (DbCommon dbConn = CommonConfigMgr.GetConnFromPool(dbConnStr, dataSourceConfig.Schema))
{
    try
    {
        dbConn.Open();
        // Connection validation here
        
        log.Debug("before constucting the oracle procedures... Line: 453");
        var cmd = dbConn.CreateDbCommand("begin :username := eokutil.f_get_username_from_udcid(:udcid, :use_gobumap);end;", CommandType.Text);
        dbConn.AddCommandParameter(":username", username, ParameterDirection.Output);
        // Add remaining parameters, then execute
        cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        log.Error($"Failed to execute Oracle procedure: {ex.Message}", ex);
        throw; // Re-throw if you need upstream error handling
    }
}

Also, check if any other code (like background threads or utility methods) is accidentally closing/disposing the connection before you run your command.

4. Check Database and Network Status

  • Confirm the Oracle database is running: Try connecting via SQL Developer or sqlplus using the same credentials and connection details.
  • Verify the Oracle Listener is active: Run lsnrctl status on the database server to check if it's accepting connections.
  • Rule out firewall issues: Make sure the port your Oracle instance uses (default 1521) is open between your app server and database server.

5. Verify Driver Compatibility

If you're using ODP.NET, ensure your driver version matches your Oracle database version. Outdated drivers can struggle to maintain connections with newer Oracle releases—check Oracle's compatibility docs and update your driver if needed.


内容的提问来源于stack exchange,提问作者SubhenduGN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:40:24