summaryrefslogtreecommitdiff
path: root/java/demo/Database/library/SQLRequestContext.java
diff options
context:
space:
mode:
authorMatthew Newhook <matthew@zeroc.com>2008-08-22 11:34:11 -0230
committerMatthew Newhook <matthew@zeroc.com>2008-08-22 11:34:11 -0230
commitb6477cc39b9fc204c9bd960ce893dfaed32c8d47 (patch)
tree5d95b60d462fb21da2b32c51f60b4c6884d21640 /java/demo/Database/library/SQLRequestContext.java
parentmore fixes to JDBC demo (diff)
downloadice-b6477cc39b9fc204c9bd960ce893dfaed32c8d47.tar.bz2
ice-b6477cc39b9fc204c9bd960ce893dfaed32c8d47.tar.xz
ice-b6477cc39b9fc204c9bd960ce893dfaed32c8d47.zip
Moved to using a dispatch interceptor model.
Diffstat (limited to 'java/demo/Database/library/SQLRequestContext.java')
-rw-r--r--java/demo/Database/library/SQLRequestContext.java118
1 files changed, 68 insertions, 50 deletions
diff --git a/java/demo/Database/library/SQLRequestContext.java b/java/demo/Database/library/SQLRequestContext.java
index e6775ffa529..114c7a0558f 100644
--- a/java/demo/Database/library/SQLRequestContext.java
+++ b/java/demo/Database/library/SQLRequestContext.java
@@ -55,55 +55,29 @@ class SQLRequestContext
public void
obtain()
{
- // Remove the current context from the map.
- synchronized(_contextMap)
+ if(_trace)
{
- _contextMap.remove(Thread.currentThread());
+ _logger.trace("SQLRequestContext", "obtain context: " + this +
+ " thread: " + Thread.currentThread());
}
+ _obtain = true;
}
public void
- destroy()
+ destroy(boolean commit)
{
- synchronized(_contextMap)
- {
- SQLRequestContext context = _contextMap.remove(Thread.currentThread());
- assert context == null;
- }
-
- // Release all resources.
- try
- {
- // Rollback the transaction if it was not committed.
- if(!_commit)
- {
- _conn.rollback();
- }
-
- java.util.Iterator<java.sql.Statement> p = _statements.iterator();
- while(p.hasNext())
- {
- p.next().close();
- }
- }
- catch(java.sql.SQLException e)
- {
- error(e);
- }
-
- _pool.release(_conn);
-
- _statements.clear();
- _conn = null;
- _pool = null;
+ assert _obtain;
+ destroyInternal(commit);
}
public void
- commit()
- throws java.sql.SQLException
+ error(String prefix, Exception ex)
{
- _conn.commit();
- _commit = true;
+ 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)
@@ -111,33 +85,76 @@ class SQLRequestContext
_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 by the servant locator.
+ // Called only during the dispatch process.
void
- destroyFromLocator()
+ destroyFromDispatch(boolean commit)
{
synchronized(_contextMap)
{
// Remove the current context from the map.
SQLRequestContext context = _contextMap.remove(Thread.currentThread());
- assert context == this;
+ assert context != null;
+ }
+
+ if(!_obtain)
+ {
+ destroyInternal(commit);
}
- destroy();
}
private void
- error(Exception ex)
+ destroyInternal(boolean commit)
{
- java.io.StringWriter sw = new java.io.StringWriter();
- java.io.PrintWriter pw = new java.io.PrintWriter(sw);
- ex.printStackTrace(pw);
- pw.flush();
- _logger.error("SQLRequestContext: error:\n" + sw.toString());
+ // 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.
@@ -145,8 +162,9 @@ class SQLRequestContext
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 _commit = false;
+ private boolean _obtain = false;
}