blob: d2f772b41cb6f7d1a179b4edcc1e5a488148a5f9 (
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
|
// **********************************************************************
//
// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <Ice/Ice.h>
#include <CurrentSqlContext.h>
#include <sqlca.h>
#include <Util.h>
using namespace std;
#ifndef TRACE
// # define TRACE 1
#endif
#ifdef _MSC_VER
#define __thread __declspec(thread)
#endif
namespace
{
//
// Each CurrentSqlContext object gets its own slot (index) in the vector
// (and there is a separate vector in each thread)
//
__thread std::vector<sql_context>* _current = 0;
size_t _currentIndex = 0;
class Notification : public Ice::ThreadNotification
{
public:
Notification(size_t index) :
_index(index)
{
}
virtual void start()
{
}
virtual void stop()
{
if(_current != 0 && _index < _current->size())
{
#ifdef TRACE
cerr << "Disconnecting from Oracle in thread " << IceUtil::ThreadControl().id() << endl;
#endif
EXEC SQL BEGIN DECLARE SECTION;
sql_context ctx = (*_current)[_index];
EXEC SQL END DECLARE SECTION;
if(ctx != 0)
{
(*_current)[_index] = 0;
EXEC SQL CONTEXT USE :ctx;
sqlca sqlca;
EXEC SQL ROLLBACK RELEASE;
EXEC SQL CONTEXT FREE :ctx;
}
if(find_if(_current->begin(), _current->end(), bind2nd(not_equal_to<sql_context>(), static_cast<void*>(0)))
== _current->end())
{
#ifdef TRACE
cerr << "Deleting _current in thread " << IceUtil::ThreadControl().id() << endl;
#endif
delete _current;
_current = 0;
}
}
}
private:
const size_t _index;
};
}
namespace
{
IceUtil::Mutex* _globalMutex = 0;
class Init
{
public:
Init()
{
_globalMutex = new IceUtil::Mutex();
}
~Init()
{
delete _globalMutex;
_globalMutex = 0;
}
};
Init init;
}
CurrentSqlContext::CurrentSqlContext(const string& connectInfo) :
_connectInfo(connectInfo)
{
{
IceUtil::Mutex::Lock lock(*_globalMutex);
_index = _currentIndex++;
}
_hook = new Notification(_index);
}
Ice::ThreadNotificationPtr
CurrentSqlContext::getHook() const
{
return _hook;
}
CurrentSqlContext::operator sql_context() const
{
if(_current == 0)
{
_current = new std::vector<sql_context>(_index + 1);
}
if(_index >= _current->size())
{
_current->resize(_index + 1);
}
EXEC SQL BEGIN DECLARE SECTION;
sql_context ctx = (*_current)[_index];
const char* connectInfo = _connectInfo.c_str();
EXEC SQL END DECLARE SECTION;
EXEC SQL WHENEVER SQLERROR DO handleSqlError(sqlca, ctx);
if(ctx == 0)
{
#ifdef TRACE
cerr << "Connecting to Oracle in thread " << IceUtil::ThreadControl().id() << endl;
#endif
//
// Allocate and connect
//
sqlca sqlca;
EXEC SQL CONTEXT ALLOCATE :ctx;
EXEC SQL CONTEXT USE :ctx;
EXEC SQL CONNECT :connectInfo;
(*_current)[_index] = ctx;
}
return ctx;
}
|