summaryrefslogtreecommitdiff
path: root/java/demo/Database/library
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
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')
-rw-r--r--java/demo/Database/library/BookI.java42
-rw-r--r--java/demo/Database/library/BookQueryResultI.java22
-rw-r--r--java/demo/Database/library/ConnectionPool.java53
-rw-r--r--java/demo/Database/library/Glacier2SessionManagerI.java8
-rw-r--r--java/demo/Database/library/Library.ice10
-rw-r--r--java/demo/Database/library/LibraryI.java64
-rw-r--r--java/demo/Database/library/SQLRequestContext.java118
-rw-r--r--java/demo/Database/library/Server.java80
-rw-r--r--java/demo/Database/library/SessionFactoryI.java8
-rw-r--r--java/demo/Database/library/SessionI.java64
-rw-r--r--java/demo/Database/library/SessionSQLRequestContext.java24
11 files changed, 260 insertions, 233 deletions
diff --git a/java/demo/Database/library/BookI.java b/java/demo/Database/library/BookI.java
index 9e8893f2989..ff7a1ee10a5 100644
--- a/java/demo/Database/library/BookI.java
+++ b/java/demo/Database/library/BookI.java
@@ -34,9 +34,7 @@ class BookI extends _BookDisp
}
catch(java.sql.SQLException e)
{
- // Log the error, and raise an UnknownException.
- error(e);
- Ice.UnknownException ex = new Ice.UnknownException();
+ JDBCException ex = new JDBCException();
ex.initCause(e);
throw ex;
}
@@ -62,9 +60,7 @@ class BookI extends _BookDisp
}
catch(java.sql.SQLException e)
{
- // Log the error, and raise an UnknownException.
- error(e);
- Ice.UnknownException ex = new Ice.UnknownException();
+ JDBCException ex = new JDBCException();
ex.initCause(e);
throw ex;
}
@@ -89,9 +85,7 @@ class BookI extends _BookDisp
}
catch(java.sql.SQLException e)
{
- // Log the error, and raise an UnknownException.
- error(e);
- Ice.UnknownException ex = new Ice.UnknownException();
+ JDBCException ex = new JDBCException();
ex.initCause(e);
throw ex;
}
@@ -130,9 +124,7 @@ class BookI extends _BookDisp
}
catch(java.sql.SQLException e)
{
- // Log the error, and raise an UnknownException.
- error(e);
- Ice.UnknownException ex = new Ice.UnknownException();
+ JDBCException ex = new JDBCException();
ex.initCause(e);
throw ex;
}
@@ -191,15 +183,10 @@ class BookI extends _BookDisp
stmt.setInt(2, id);
int count = stmt.executeUpdate();
assert count == 1;
-
- // Commit the transaction.
- context.commit();
}
catch(java.sql.SQLException e)
{
- // Log the error, and raise an UnknownException.
- error(e);
- Ice.UnknownException ex = new Ice.UnknownException();
+ JDBCException ex = new JDBCException();
ex.initCause(e);
throw ex;
}
@@ -234,17 +221,14 @@ class BookI extends _BookDisp
}
catch(java.sql.SQLException e)
{
- // Log the error, and raise an UnknownException.
- error(e);
- Ice.UnknownException ex = new Ice.UnknownException();
+ JDBCException ex = new JDBCException();
ex.initCause(e);
throw ex;
}
}
- BookI(Ice.Logger logger)
+ BookI()
{
- _logger = logger;
}
static Ice.Identity
@@ -293,16 +277,4 @@ class BookI extends _BookDisp
return desc;
}
-
- private void
- error(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("BookI: error:\n" + sw.toString());
- }
-
- private Ice.Logger _logger;
}
diff --git a/java/demo/Database/library/BookQueryResultI.java b/java/demo/Database/library/BookQueryResultI.java
index a3a159a48e4..7d2a9ac9ab9 100644
--- a/java/demo/Database/library/BookQueryResultI.java
+++ b/java/demo/Database/library/BookQueryResultI.java
@@ -11,11 +11,10 @@ import Demo.*;
class BookQueryResultI extends _BookQueryResultDisp
{
- // The query result owns the java.sql.Connection object until
+ // The query result owns the SQLRequestContext object until
// destroyed.
- BookQueryResultI(Ice.Logger logger, SQLRequestContext context, java.sql.ResultSet rs)
+ BookQueryResultI(SQLRequestContext context, java.sql.ResultSet rs)
{
- _logger = logger;
_context = context;
_rs = rs;
}
@@ -45,7 +44,7 @@ class BookQueryResultI extends _BookQueryResultDisp
catch(java.sql.SQLException e)
{
// Log the error, and raise an UnknownException.
- error(e);
+ _context.error("BookQueryResultI", e);
Ice.UnknownException ex = new Ice.UnknownException();
ex.initCause(e);
throw ex;
@@ -75,7 +74,7 @@ class BookQueryResultI extends _BookQueryResultDisp
throw new Ice.ObjectNotExistException();
}
_destroyed = true;
- _context.destroy();
+ _context.destroy(false);
current.adapter.remove(current.id);
}
@@ -87,21 +86,10 @@ class BookQueryResultI extends _BookQueryResultDisp
if(!_destroyed)
{
_destroyed = true;
- _context.destroy();
+ _context.destroy(false);
}
}
- private void
- error(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("BookQueryResultI: error:\n" + sw.toString());
- }
-
- private Ice.Logger _logger;
private SQLRequestContext _context;
private java.sql.ResultSet _rs;
private boolean _destroyed = false;
diff --git a/java/demo/Database/library/ConnectionPool.java b/java/demo/Database/library/ConnectionPool.java
index de9d1cd6aee..42391dec1e5 100644
--- a/java/demo/Database/library/ConnectionPool.java
+++ b/java/demo/Database/library/ConnectionPool.java
@@ -9,24 +9,6 @@
class ConnectionPool
{
- public
- ConnectionPool(Ice.Logger logger, String url, String username, String password, int numConnections)
- throws java.sql.SQLException
- {
- _logger = logger;
- _url = url;
- _username = username;
- _password = password;
-
- _nconnections = numConnections;
- while(numConnections-- > 0)
- {
- java.sql.Connection connection = java.sql.DriverManager.getConnection(url, username, password);
- connection.setAutoCommit(false);
- _connections.add(connection);
- }
- }
-
public synchronized void
destroy()
{
@@ -98,7 +80,10 @@ class ConnectionPool
// we need to re-establish the connection.
while(conn == null)
{
- _logger.trace("ConnectionPool", "establishing new database connection");
+ if(_trace)
+ {
+ _logger.trace("ConnectionPool", "establishing new database connection");
+ }
try
{
conn = java.sql.DriverManager.getConnection(_url, _username, _password);
@@ -113,6 +98,11 @@ class ConnectionPool
_logger.warning("ConnectionPool: database connection failed:\n" + sw.toString());
}
}
+ if(_trace)
+ {
+ _logger.trace("ConnectionPool", "returning connection: " + conn + " " +
+ _connections.size() + "/" + _nconnections + " remaining");
+ }
return conn;
}
@@ -126,7 +116,30 @@ class ConnectionPool
}
}
- Ice.Logger _logger;
+ ConnectionPool(Ice.Logger logger, String url, String username, String password, int numConnections)
+ throws java.sql.SQLException
+ {
+ _logger = logger;
+ _url = url;
+ _username = username;
+ _password = password;
+
+ _nconnections = numConnections;
+ if(_trace)
+ {
+ _logger.trace("ConnectionPool", "establishing " + numConnections + " connections to " + url);
+ }
+ while(numConnections-- > 0)
+ {
+ java.sql.Connection connection = java.sql.DriverManager.getConnection(url, username, password);
+ connection.setAutoCommit(false);
+ _connections.add(connection);
+ }
+ }
+
+
+ private Ice.Logger _logger;
+ private boolean _trace = false;
private String _url;
private String _username;
private String _password;
diff --git a/java/demo/Database/library/Glacier2SessionManagerI.java b/java/demo/Database/library/Glacier2SessionManagerI.java
index 1c3ba18faa0..15e3cc487ca 100644
--- a/java/demo/Database/library/Glacier2SessionManagerI.java
+++ b/java/demo/Database/library/Glacier2SessionManagerI.java
@@ -14,7 +14,7 @@ class Glacier2SessionManagerI extends Glacier2._SessionManagerDisp
public Glacier2.SessionPrx
create(String userId, Glacier2.SessionControlPrx control, Ice.Current c)
{
- SessionI session = new SessionI(_logger, c.adapter);
+ SessionI session = new SessionI(_logger, _pool, c.adapter, _libraryServant);
_Glacier2SessionTie servant = new _Glacier2SessionTie(session);
Glacier2.SessionPrx proxy = Glacier2.SessionPrxHelper.uncheckedCast(c.adapter.addWithUUID(servant));
_logger.trace("SessionFactory", "create new session: " +
@@ -23,12 +23,16 @@ class Glacier2SessionManagerI extends Glacier2._SessionManagerDisp
return proxy;
}
- Glacier2SessionManagerI(Ice.Logger logger, ReapThread reaper)
+ Glacier2SessionManagerI(Ice.Logger logger, ReapThread reaper, ConnectionPool pool, Ice.Object libraryServant)
{
_logger = logger;
_reaper = reaper;
+ _pool = pool;
+ _libraryServant = libraryServant;
}
private Ice.Logger _logger;
private ReapThread _reaper;
+ private ConnectionPool _pool;
+ private Ice.Object _libraryServant;
}
diff --git a/java/demo/Database/library/Library.ice b/java/demo/Database/library/Library.ice
index 16c069e661c..9ce656d4829 100644
--- a/java/demo/Database/library/Library.ice
+++ b/java/demo/Database/library/Library.ice
@@ -17,6 +17,16 @@ module Demo
/**
*
+ * This local exception is used internally if a java.sql.SQLException
+ * is raised.
+ *
+ **/
+local exception JDBCException
+{
+};
+
+/**
+ *
* This exception is raised if the book already exists.
*
**/
diff --git a/java/demo/Database/library/LibraryI.java b/java/demo/Database/library/LibraryI.java
index 7f01f9f87c6..ac160b629cc 100644
--- a/java/demo/Database/library/LibraryI.java
+++ b/java/demo/Database/library/LibraryI.java
@@ -20,17 +20,11 @@ class LibraryI extends _LibraryDisp
queryByIsbn(String isbn, BookDescriptionHolder first, BookQueryResultPrxHolder result, Ice.Current current)
throws NoResultsException
{
- SessionI session = SessionI.getSession(current.id);
- if(session == null)
- {
- // No associated session.
- throw new Ice.ObjectNotExistException();
- }
- session.reapQueries();
-
- SQLRequestContext context = SQLRequestContext.getCurrentContext();
+ SessionSQLRequestContext context = (SessionSQLRequestContext)SQLRequestContext.getCurrentContext();
assert context != null;
+ context.getSession().reapQueries();
+
try
{
java.sql.PreparedStatement stmt = context.prepareStatement("SELECT * FROM books WHERE isbn LIKE ?");
@@ -47,16 +41,14 @@ class LibraryI extends _LibraryDisp
// The SQLRequestContext is now owned by the query
// implementation.
context.obtain();
- BookQueryResultI impl = new BookQueryResultI(_logger, context, rs);
+ BookQueryResultI impl = new BookQueryResultI(context, rs);
result.value = BookQueryResultPrxHelper.uncheckedCast(current.adapter.addWithUUID(impl));
- session.add(result.value, impl);
+ context.getSession().add(result.value, impl);
}
}
catch(java.sql.SQLException e)
{
- // Log the error, and raise an UnknownException.
- error(e);
- Ice.UnknownException ex = new Ice.UnknownException();
+ JDBCException ex = new JDBCException();
ex.initCause(e);
throw ex;
}
@@ -66,17 +58,11 @@ class LibraryI extends _LibraryDisp
queryByAuthor(String author, BookDescriptionHolder first, BookQueryResultPrxHolder result, Ice.Current current)
throws NoResultsException
{
- SessionI session = SessionI.getSession(current.id);
- if(session == null)
- {
- // No associated session.
- throw new Ice.ObjectNotExistException();
- }
- session.reapQueries();
-
- SQLRequestContext context = SQLRequestContext.getCurrentContext();
+ SessionSQLRequestContext context = (SessionSQLRequestContext)SQLRequestContext.getCurrentContext();
assert context != null;
+ context.getSession().reapQueries();
+
try
{
// Find each of the authors.
@@ -118,16 +104,14 @@ class LibraryI extends _LibraryDisp
{
// The SQLRequestContext is now owned by the query implementation.
context.obtain();
- BookQueryResultI impl = new BookQueryResultI(_logger, context, rs);
+ BookQueryResultI impl = new BookQueryResultI(context, rs);
result.value = BookQueryResultPrxHelper.uncheckedCast(current.adapter.addWithUUID(impl));
- session.add(result.value, impl);
+ context.getSession().add(result.value, impl);
}
}
catch(java.sql.SQLException e)
{
- // Log the error, and raise an UnknownException.
- error(e);
- Ice.UnknownException ex = new Ice.UnknownException();
+ JDBCException ex = new JDBCException();
ex.initCause(e);
throw ex;
}
@@ -137,7 +121,7 @@ class LibraryI extends _LibraryDisp
createBook(String isbn, String title, java.util.List<String> authors, Ice.Current current)
throws BookExistsException
{
- SQLRequestContext context = SQLRequestContext.getCurrentContext();
+ SessionSQLRequestContext context = (SessionSQLRequestContext)SQLRequestContext.getCurrentContext();
assert context != null;
try
{
@@ -211,35 +195,17 @@ class LibraryI extends _LibraryDisp
assert count == 1;
}
- // Commit the transaction.
- context.commit();
-
return BookPrxHelper.uncheckedCast(current.adapter.createProxy(BookI.createIdentity(bookId)));
}
catch(java.sql.SQLException e)
{
- // Log the error, and raise an UnknownException.
- error(e);
- Ice.UnknownException ex = new Ice.UnknownException();
+ JDBCException ex = new JDBCException();
ex.initCause(e);
throw ex;
}
}
- LibraryI(Ice.Logger logger)
- {
- _logger = logger;
- }
-
- private void
- error(Exception ex)
+ LibraryI()
{
- java.io.StringWriter sw = new java.io.StringWriter();
- java.io.PrintWriter pw = new java.io.PrintWriter(sw);
- ex.printStackTrace(pw);
- pw.flush();
- _logger.error("LibraryI: error:\n" + sw.toString());
}
-
- private Ice.Logger _logger;
}
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;
}
diff --git a/java/demo/Database/library/Server.java b/java/demo/Database/library/Server.java
index 2b8d9aa0af5..b95bb76b56c 100644
--- a/java/demo/Database/library/Server.java
+++ b/java/demo/Database/library/Server.java
@@ -11,46 +11,61 @@ import Demo.*;
class LibraryServer extends Ice.Application
{
- static class LocatorI implements Ice.ServantLocator
+ static class BookDispatchInterceptorI extends Ice.DispatchInterceptor
{
- LocatorI(Ice.Logger logger, ConnectionPool pool, Ice.Object bookServant, Ice.Object libraryServant)
+ BookDispatchInterceptorI(Ice.Logger logger, ConnectionPool pool, Ice.Object servant)
{
_logger = logger;
_pool = pool;
- _bookServant = bookServant;
- _libraryServant = libraryServant;
+ _servant = servant;
}
- public Ice.Object
- locate(Ice.Current c, Ice.LocalObjectHolder cookie)
+ public Ice.DispatchStatus
+ dispatch(Ice.Request request)
{
- assert c.id.category.equals("library") || c.id.category.equals("book");
-
- // Setup the new SQL request context.
SQLRequestContext context = new SQLRequestContext(_logger, _pool);
-
- if(c.id.category.equals("library"))
+ try
{
- return _libraryServant;
+ Ice.DispatchStatus status = _servant.ice_dispatch(request, null);
+ context.destroyFromDispatch(status == Ice.DispatchStatus.DispatchOK);
+ return status;
}
- else
+ catch(JDBCException ex)
{
- assert(c.id.category.equals("book"));
- return _bookServant;
+ context.error("dispatch to " +
+ request.getCurrent().id.category + "/" + request.getCurrent().id.name +
+ " failed.", ex);
+ context.destroyFromDispatch(false);
+
+ // Translate the exception to UnknownException.
+ Ice.UnknownException e = new Ice.UnknownException();
+ ex.initCause(e);
+ throw e;
}
}
+ private Ice.Logger _logger;
+ private ConnectionPool _pool;
+ private Ice.Object _servant;
+ }
+
+ static class LocatorI implements Ice.ServantLocator
+ {
+ LocatorI(Ice.Logger logger, ConnectionPool pool, Ice.Object servant)
+ {
+ _servant = new BookDispatchInterceptorI(logger, pool, servant);
+ }
+
+ public Ice.Object
+ locate(Ice.Current c, Ice.LocalObjectHolder cookie)
+ {
+ assert c.id.category.equals("book");
+ return _servant;
+ }
+
public void
finished(Ice.Current c, Ice.Object servant, Object cookie)
{
- // If a SQL request context is still associated with this
- // request, then destroy it (it will not be associated if
- // obtain was called).
- SQLRequestContext context = SQLRequestContext.getCurrentContext();
- if(context != null)
- {
- context.destroyFromLocator();
- }
}
public void
@@ -58,16 +73,14 @@ class LibraryServer extends Ice.Application
{
}
- private Ice.Logger _logger;
- private ConnectionPool _pool;
-
- private Ice.Object _bookServant;
- private Ice.Object _libraryServant;
+ private Ice.Object _servant;
}
public int
run(String[] args)
{
+ args = communicator().getProperties().parseCommandLineOptions("JDBC", args);
+
if(args.length > 0)
{
System.err.println(appName() + ": too many arguments");
@@ -123,14 +136,15 @@ class LibraryServer extends Ice.Application
//
Ice.ObjectAdapter adapter = communicator().createObjectAdapter("SessionFactory");
- LocatorI locator = new LocatorI(logger, pool, new BookI(logger), new LibraryI(logger));
-
- adapter.add(new SessionFactoryI(logger, reaper), communicator().stringToIdentity("SessionFactory"));
- adapter.add(new Glacier2SessionManagerI(logger, reaper),
+ LocatorI locator = new LocatorI(logger, pool, new BookI());
+ Ice.Object library = new LibraryI();
+
+ adapter.add(new SessionFactoryI(logger, reaper, pool, library),
+ communicator().stringToIdentity("SessionFactory"));
+ adapter.add(new Glacier2SessionManagerI(logger, reaper, pool, library),
communicator().stringToIdentity("LibrarySessionManager"));
adapter.addServantLocator(locator, "book");
- adapter.addServantLocator(locator, "library");
//
// Everything ok, let's go.
diff --git a/java/demo/Database/library/SessionFactoryI.java b/java/demo/Database/library/SessionFactoryI.java
index cb365b576a7..50957204ea9 100644
--- a/java/demo/Database/library/SessionFactoryI.java
+++ b/java/demo/Database/library/SessionFactoryI.java
@@ -14,7 +14,7 @@ class SessionFactoryI extends _SessionFactoryDisp
public synchronized SessionPrx
create(Ice.Current c)
{
- SessionI session = new SessionI(_logger, c.adapter);
+ SessionI session = new SessionI(_logger, _pool, c.adapter, _libraryServant);
_SessionTie servant = new _SessionTie(session);
SessionPrx proxy = SessionPrxHelper.uncheckedCast(c.adapter.addWithUUID(servant));
_logger.trace("SessionFactory", "create new session: " +
@@ -23,12 +23,16 @@ class SessionFactoryI extends _SessionFactoryDisp
return proxy;
}
- SessionFactoryI(Ice.Logger logger, ReapThread reaper)
+ SessionFactoryI(Ice.Logger logger, ReapThread reaper, ConnectionPool pool, Ice.Object libraryServant)
{
_logger = logger;
_reaper = reaper;
+ _pool = pool;
+ _libraryServant = libraryServant;
}
private Ice.Logger _logger;
private ReapThread _reaper;
+ private ConnectionPool _pool;
+ private Ice.Object _libraryServant;
}
diff --git a/java/demo/Database/library/SessionI.java b/java/demo/Database/library/SessionI.java
index e7cc744ed80..bd460271e48 100644
--- a/java/demo/Database/library/SessionI.java
+++ b/java/demo/Database/library/SessionI.java
@@ -11,13 +11,44 @@ import Demo.*;
class SessionI implements _SessionOperations, _Glacier2SessionOperations
{
- static public SessionI
- getSession(Ice.Identity id)
+ static class SessionDispatchInterceptorI extends Ice.DispatchInterceptor
{
- synchronized(_sessions)
+ SessionDispatchInterceptorI(Ice.Logger logger, ConnectionPool pool, Ice.Object servant, SessionI session)
{
- return _sessions.get(id);
+ _logger = logger;
+ _pool = pool;
+ _servant = servant;
+ _session = session;
}
+
+ public Ice.DispatchStatus
+ dispatch(Ice.Request request)
+ {
+ SessionSQLRequestContext context = new SessionSQLRequestContext(_session, _logger, _pool);
+ try
+ {
+ Ice.DispatchStatus status = _servant.ice_dispatch(request, null);
+ context.destroyFromDispatch(status == Ice.DispatchStatus.DispatchOK);
+ return status;
+ }
+ catch(JDBCException ex)
+ {
+ context.error("dispatch to " +
+ request.getCurrent().id.category + "/" + request.getCurrent().id.name +
+ " failed.", ex);
+ context.destroyFromDispatch(false);
+
+ // Translate the exception to UnknownException.
+ Ice.UnknownException e = new Ice.UnknownException();
+ ex.initCause(e);
+ throw e;
+ }
+ }
+
+ private Ice.Logger _logger;
+ private ConnectionPool _pool;
+ private Ice.Object _servant;
+ private SessionI _session;
}
synchronized public LibraryPrx
@@ -52,13 +83,6 @@ class SessionI implements _SessionOperations, _Glacier2SessionOperations
_logger.trace("Session", "session " + c.adapter.getCommunicator().identityToString(c.id) +
" is now destroyed.");
- // Remove the session from the sessions map.
- synchronized(_sessions)
- {
- Object o = _sessions.remove(_library.ice_getIdentity());
- assert o != null;
- }
-
java.util.Iterator<QueryProxyPair> p = _queries.iterator();
while(p.hasNext())
{
@@ -73,6 +97,7 @@ class SessionI implements _SessionOperations, _Glacier2SessionOperations
}
// This method is never called on shutdown of the server.
+ c.adapter.remove(_library.ice_getIdentity());
c.adapter.remove(c.id);
}
@@ -139,21 +164,13 @@ class SessionI implements _SessionOperations, _Glacier2SessionOperations
}
}
- SessionI(Ice.Logger logger, Ice.ObjectAdapter adapter)
+ SessionI(Ice.Logger logger, ConnectionPool pool, Ice.ObjectAdapter adapter, Ice.Object libraryServant)
{
_logger = logger;
_timestamp = System.currentTimeMillis();
- Ice.Identity id = new Ice.Identity();
- id.category = "library";
- id.name = Ice.Util.generateUUID();
- _library = LibraryPrxHelper.uncheckedCast(adapter.createProxy(id));
-
- // Add the library to the sessions map.
- synchronized(_sessions)
- {
- _sessions.put(_library.ice_getIdentity(), this);
- }
+ _library = LibraryPrxHelper.uncheckedCast(
+ adapter.addWithUUID(new SessionDispatchInterceptorI(logger, pool, libraryServant, this)));
}
static class QueryProxyPair
@@ -168,9 +185,6 @@ class SessionI implements _SessionOperations, _Glacier2SessionOperations
BookQueryResultI impl;
}
- private static java.util.Map<Ice.Identity, SessionI> _sessions =
- new java.util.HashMap<Ice.Identity, SessionI>();
-
private java.util.List<QueryProxyPair> _queries = new java.util.LinkedList<QueryProxyPair>();
private Ice.Logger _logger;
private boolean _destroyed = false; // true if destroy() was called, false otherwise.
diff --git a/java/demo/Database/library/SessionSQLRequestContext.java b/java/demo/Database/library/SessionSQLRequestContext.java
new file mode 100644
index 00000000000..799ce4a92f6
--- /dev/null
+++ b/java/demo/Database/library/SessionSQLRequestContext.java
@@ -0,0 +1,24 @@
+// **********************************************************************
+//
+// 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.
+//
+// **********************************************************************
+
+class SessionSQLRequestContext extends SQLRequestContext
+{
+ SessionSQLRequestContext(SessionI session, Ice.Logger logger, ConnectionPool pool)
+ {
+ super(logger, pool);
+ _session = session;
+ }
+
+ SessionI getSession()
+ {
+ return _session;
+ }
+
+ private SessionI _session;
+}