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
|
// **********************************************************************
//
// Copyright (c) 2003-2008 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.
//
// **********************************************************************
//
// A SQL request context encapsulates SQL resources allocated in the
// process of executing a request, such as the database connection,
// and associated SQL statements.
//
// The request context is automatically destroyed at the end of a
// request, or if obtain is called it must be destroyed manually by
// calling destroy.
//
// When the request context is destroyed, the transaction is rolled
// back, if not already committed, and all allocated resources are
// released,
//
class SQLRequestContext
{
public static SQLRequestContext
getCurrentContext()
{
synchronized(_contextMap)
{
return _contextMap.get(Thread.currentThread());
}
}
public java.sql.PreparedStatement
prepareStatement(String sql)
throws java.sql.SQLException
{
java.sql.PreparedStatement stmt = _conn.prepareStatement(sql);
_statements.add(stmt);
return stmt;
}
public java.sql.PreparedStatement
prepareStatement(String sql, int autoGeneratedKeys)
throws java.sql.SQLException
{
java.sql.PreparedStatement stmt = _conn.prepareStatement(sql, autoGeneratedKeys);
_statements.add(stmt);
return stmt;
}
// Called to obtain ownership of the context. The context is no
// longer destroyed automatically when the current request has
// completed.
public void
obtain()
{
if(_trace)
{
_logger.trace("SQLRequestContext", "obtain context: " + this +
" thread: " + Thread.currentThread());
}
_obtain = true;
}
public void
destroy(boolean commit)
{
assert _obtain;
destroyInternal(commit);
}
public void
error(String prefix, Exception ex)
{
java.io.StringWriter sw = new java.io.StringWriter();
java.io.PrintWriter pw = new java.io.PrintWriter(sw);
ex.printStackTrace(pw);
pw.flush();
_logger.error(prefix + ": error:\n" + sw.toString());
}
SQLRequestContext(Ice.Logger logger, ConnectionPool pool)
{
_logger = logger;
_pool = pool;
_conn = pool.acquire();
synchronized(_contextMap)
{
if(_trace)
{
_logger.trace("SQLRequestContext", "create new context: " + this +
" thread: " + Thread.currentThread() +
": connection: " + _conn);
}
_contextMap.put(Thread.currentThread(), this);
}
}
// Called only during the dispatch process.
void
destroyFromDispatch(boolean commit)
{
synchronized(_contextMap)
{
// Remove the current context from the map.
SQLRequestContext context = _contextMap.remove(Thread.currentThread());
assert context != null;
}
if(!_obtain)
{
destroyInternal(commit);
}
}
private void
destroyInternal(boolean commit)
{
// Release all resources.
try
{
if(commit)
{
_conn.commit();
if(_trace)
{
_logger.trace("SQLRequestContext", "commit context: " + this);
}
}
else
{
_conn.rollback();
if(_trace)
{
_logger.trace("SQLRequestContext", "rollback context: " + this);
}
}
java.util.Iterator<java.sql.Statement> p = _statements.iterator();
while(p.hasNext())
{
p.next().close();
}
}
catch(java.sql.SQLException e)
{
error("SQLRequestContext", e);
}
_pool.release(_conn);
_statements.clear();
_conn = null;
_pool = null;
}
// A map of threads to request contexts.
private static java.util.Map<Thread, SQLRequestContext> _contextMap =
new java.util.HashMap<Thread, SQLRequestContext>();
private Ice.Logger _logger;
private boolean _trace = false;
private ConnectionPool _pool;
private java.util.List<java.sql.Statement> _statements = new java.util.LinkedList<java.sql.Statement>();
private java.sql.Connection _conn;
private boolean _obtain = false;
}
|