aboutsummaryrefslogtreecommitdiff
path: root/odb/oracle/error.cxx
blob: 7989060ea8a41ca9aba08e43a06783f1dca1c76b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// file      : odb/oracle/error.cxx
// copyright : Copyright (c) 2005-2017 Code Synthesis Tools CC
// license   : ODB NCUEL; see accompanying LICENSE file

#include <oci.h>

#include <cstring> // std::strlen
#include <cassert>

#include <odb/details/buffer.hxx>

#include <odb/oracle/error.hxx>
#include <odb/oracle/exceptions.hxx>
#include <odb/oracle/connection.hxx>

using namespace std;

namespace odb
{
  namespace oracle
  {
    static void
    translate_error (void* h, ub4 htype, sword r, connection* conn,
                     size_t pos, multiple_exceptions* mex)
    {
      assert (r != OCI_SUCCESS && r != OCI_SUCCESS_WITH_INFO);

      switch (r)
      {
      case OCI_STILL_EXECUTING:
        {
          throw database_exception (0, "statement still executing");
          break;
          }
      case OCI_NEED_DATA:
      case OCI_NO_DATA:
        {
          throw database_exception (0, "unhandled OCI_*_DATA condition");
          break;
        }
      case OCI_INVALID_HANDLE:
        {
          throw invalid_oci_handle ();
          break;
        }
      default:
        {
          break;
        }
      }

      sb4 e;
      char b[512];  // Error message will be truncated if it does not fit.

      if (htype == OCI_HTYPE_ERROR)
      {
        // We need to translate certain Oracle error codes to special
        // exceptions, such as deadlock, timeout, etc. The problem is we can
        // have multiple records potentially with different error codes. If we
        // have both, say, a deadlock code and some other code, then we should
        // probably throw database_exception, which is more severe. To
        // implement this we are going to pre-scan the records looking for the
        // codes we are interested in. If in the process we see any other code,
        // then we stop and go ahead to prepare and throw database_exception.
        //
        enum code
        {
          code_none,
          code_deadlock,
          code_timeout,
          code_connection_lost
        };

        code c (code_none);

        for (sb4 i (1);; ++i)
        {
          r = OCIErrorGet (h,
                           i,
                           0,
                           &e,
                           reinterpret_cast<OraText*> (b),
                           512,
                           htype);

          if (r == OCI_NO_DATA)
            break;

          code nc;

          if (e == 60 || // Deadlock detected while waiting for resource.
              e == 104)  // Deadlock detected; all public servers blocked.
            nc = code_deadlock;
          else if (e == 51 || // Timeout occurred while waiting for a resource.
                   e == 54 || // Resource busy and acquisition timeout expired.
                   e == 2049) // Distributed lock timeout.
            nc = code_timeout;
          else if (e == 28 ||   // Session has been killed.
                   e == 3113 || // End-of-file on communication channel.
                   e == 3135 || // Connection lost contact.
                   e == 3136 || // Inbound connection timed out.
                   e == 3138)   // Connection terminated.
            nc = code_connection_lost;
          else
          {
            c = code_none;
            break;
          }

          if (c != code_none && c != nc)
          {
            // Several different codes.
            //
            c = code_none;
            break;
          }

          c = nc;
        }

        // Check if the connection is lost. If code is connection_lost,
        // then we know it is gone. If code is deadlock, then the
        // connection is most likely ok.
        //
        if (conn != 0 && (c == code_none || c == code_timeout))
        {
          OCIServer* server;
          r = OCIAttrGet (conn->handle (),
                          OCI_HTYPE_SVCCTX,
                          &server,
                          0,
                          OCI_ATTR_SERVER,
                          conn->error_handle ());

          if (r != OCI_SUCCESS)
            throw invalid_oci_handle ();

          ub4 server_status;
          r = OCIAttrGet (server,
                          OCI_HTYPE_SERVER,
                          &server_status,
                          0,
                          OCI_ATTR_SERVER_STATUS,
                          conn->error_handle ());

          if (r != OCI_SUCCESS)
            throw invalid_oci_handle ();

          if (server_status == OCI_SERVER_NOT_CONNECTED)
            conn->mark_failed ();
        }

        switch (c)
        {
        case code_deadlock:
          throw deadlock ();
        case code_timeout:
          throw timeout ();
        case code_connection_lost:
          {
            if (conn != 0)
              conn->mark_failed ();

            throw connection_lost ();
          }
        case code_none:
          break;
        }
      }

      // Some other error code. Prepare database_exception.
      //
      database_exception dbe;

      for (sb4 i (1);; ++i)
      {
        r = OCIErrorGet (h,
                         i,
                         0,
                         &e,
                         reinterpret_cast<OraText*> (b),
                         512,
                         htype);

        if (r == OCI_NO_DATA)
          break;

        // Get rid of a trailing newline if there is one.
        //
        size_t n (strlen (b));
        if (n != 0 && b[n - 1] == '\n')
          b[n - 1] = '\0';

        dbe.append (e, b);
      }

      if (mex == 0)
        throw dbe;
      else
        // It could be that some of these errors are fatal. I guess we
        // will just have to learn from experience which ones are. The
        // client code can always treat specific error codes as fatal.
        //
        mex->insert (pos, dbe);
    }

    void
    translate_error (OCIError* h, sword r, connection* c,
                     size_t pos, multiple_exceptions* mex)
    {
      translate_error (h, OCI_HTYPE_ERROR, r, c, pos, mex);
    }

    void
    translate_error (connection& c, sword r)
    {
      translate_error (c.error_handle (), OCI_HTYPE_ERROR, r, &c, 0, 0);
    }

    void
    translate_error (OCIEnv* h)
    {
      translate_error (h, OCI_HTYPE_ENV, OCI_ERROR, 0, 0, 0);
    }
  }
}