summaryrefslogtreecommitdiff
path: root/java/demo/Database/library
diff options
context:
space:
mode:
authorMatthew Newhook <matthew@zeroc.com>2008-08-22 12:07:39 -0230
committerMatthew Newhook <matthew@zeroc.com>2008-08-22 12:07:39 -0230
commitba630ccacc8ee0b1735e263569107a5f309a3a2d (patch)
treeadbf1c264aae37bef60eb42350b203391ad3e2e1 /java/demo/Database/library
parentMoved to using a dispatch interceptor model. (diff)
downloadice-ba630ccacc8ee0b1735e263569107a5f309a3a2d.tar.bz2
ice-ba630ccacc8ee0b1735e263569107a5f309a3a2d.tar.xz
ice-ba630ccacc8ee0b1735e263569107a5f309a3a2d.zip
use a library per session.
Diffstat (limited to 'java/demo/Database/library')
-rw-r--r--java/demo/Database/library/ConnectionPool.java2
-rw-r--r--java/demo/Database/library/DispatchInterceptorI.java58
-rw-r--r--java/demo/Database/library/Glacier2SessionManagerI.java12
-rw-r--r--java/demo/Database/library/LibraryI.java23
-rw-r--r--java/demo/Database/library/SQLRequestContext.java35
-rw-r--r--java/demo/Database/library/Server.java59
-rw-r--r--java/demo/Database/library/SessionFactoryI.java12
-rw-r--r--java/demo/Database/library/SessionI.java46
-rw-r--r--java/demo/Database/library/SessionSQLRequestContext.java24
-rw-r--r--java/demo/Database/library/config.server2
10 files changed, 118 insertions, 155 deletions
diff --git a/java/demo/Database/library/ConnectionPool.java b/java/demo/Database/library/ConnectionPool.java
index 42391dec1e5..30bf5205fff 100644
--- a/java/demo/Database/library/ConnectionPool.java
+++ b/java/demo/Database/library/ConnectionPool.java
@@ -139,7 +139,7 @@ class ConnectionPool
private Ice.Logger _logger;
- private boolean _trace = false;
+ private boolean _trace = true;
private String _url;
private String _username;
private String _password;
diff --git a/java/demo/Database/library/DispatchInterceptorI.java b/java/demo/Database/library/DispatchInterceptorI.java
new file mode 100644
index 00000000000..2a8f85d72ee
--- /dev/null
+++ b/java/demo/Database/library/DispatchInterceptorI.java
@@ -0,0 +1,58 @@
+// **********************************************************************
+//
+// 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.
+//
+// **********************************************************************
+
+import Demo.*;
+
+class DispatchInterceptorI extends Ice.DispatchInterceptor
+{
+ public Ice.DispatchStatus
+ dispatch(Ice.Request request)
+ {
+ // Allocate a new SQLRequestContext associated with this
+ // request thread.
+ SQLRequestContext context = new SQLRequestContext();
+ try
+ {
+ Ice.DispatchStatus status = _servant.ice_dispatch(request, null);
+
+ // An exception causes the current transaction to rollback.
+ context.destroyFromDispatch(status == Ice.DispatchStatus.DispatchOK);
+
+ return status;
+ }
+ catch(JDBCException ex)
+ {
+ // Log the error.
+ Ice.Current c = request.getCurrent();
+ context.error("call of `" + c.operation + "' on id `" + c.id.category + "/" + c.id.name + "' failed", ex);
+
+ // A JDBCException causes the current transaction to
+ // rollback.
+ context.destroyFromDispatch(false);
+
+ // Translate the exception to UnknownException.
+ Ice.UnknownException e = new Ice.UnknownException();
+ e.initCause(ex);
+ throw e;
+ }
+ catch(RuntimeException ex)
+ {
+ // Any other exception causes the transaction to rollback.
+ context.destroyFromDispatch(false);
+ throw ex;
+ }
+ }
+
+ DispatchInterceptorI(Ice.Object servant)
+ {
+ _servant = servant;
+ }
+
+ private Ice.Object _servant;
+}
diff --git a/java/demo/Database/library/Glacier2SessionManagerI.java b/java/demo/Database/library/Glacier2SessionManagerI.java
index 15e3cc487ca..8fc96a10447 100644
--- a/java/demo/Database/library/Glacier2SessionManagerI.java
+++ b/java/demo/Database/library/Glacier2SessionManagerI.java
@@ -14,25 +14,25 @@ class Glacier2SessionManagerI extends Glacier2._SessionManagerDisp
public Glacier2.SessionPrx
create(String userId, Glacier2.SessionControlPrx control, Ice.Current c)
{
- SessionI session = new SessionI(_logger, _pool, c.adapter, _libraryServant);
+ SessionI session = new SessionI(_logger, c.adapter);
_Glacier2SessionTie servant = new _Glacier2SessionTie(session);
+
Glacier2.SessionPrx proxy = Glacier2.SessionPrxHelper.uncheckedCast(c.adapter.addWithUUID(servant));
+
_logger.trace("SessionFactory", "create new session: " +
c.adapter.getCommunicator().identityToString(proxy.ice_getIdentity()));
+
_reaper.add(proxy, session);
+
return proxy;
}
- Glacier2SessionManagerI(Ice.Logger logger, ReapThread reaper, ConnectionPool pool, Ice.Object libraryServant)
+ Glacier2SessionManagerI(Ice.Logger logger, ReapThread reaper)
{
_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/LibraryI.java b/java/demo/Database/library/LibraryI.java
index ac160b629cc..e1c52f69487 100644
--- a/java/demo/Database/library/LibraryI.java
+++ b/java/demo/Database/library/LibraryI.java
@@ -10,9 +10,7 @@
import Demo.*;
//
-// This servant is a default servant. The book identity is retrieved
-// from the Ice.Current object. The session object associated with the
-// library is also retrieved using the Library's identity.
+// This is a per-session library object.
//
class LibraryI extends _LibraryDisp
{
@@ -20,10 +18,10 @@ class LibraryI extends _LibraryDisp
queryByIsbn(String isbn, BookDescriptionHolder first, BookQueryResultPrxHolder result, Ice.Current current)
throws NoResultsException
{
- SessionSQLRequestContext context = (SessionSQLRequestContext)SQLRequestContext.getCurrentContext();
+ SQLRequestContext context = SQLRequestContext.getCurrentContext();
assert context != null;
- context.getSession().reapQueries();
+ _session.reapQueries();
try
{
@@ -43,7 +41,7 @@ class LibraryI extends _LibraryDisp
context.obtain();
BookQueryResultI impl = new BookQueryResultI(context, rs);
result.value = BookQueryResultPrxHelper.uncheckedCast(current.adapter.addWithUUID(impl));
- context.getSession().add(result.value, impl);
+ _session.add(result.value, impl);
}
}
catch(java.sql.SQLException e)
@@ -58,10 +56,10 @@ class LibraryI extends _LibraryDisp
queryByAuthor(String author, BookDescriptionHolder first, BookQueryResultPrxHolder result, Ice.Current current)
throws NoResultsException
{
- SessionSQLRequestContext context = (SessionSQLRequestContext)SQLRequestContext.getCurrentContext();
+ SQLRequestContext context = SQLRequestContext.getCurrentContext();
assert context != null;
- context.getSession().reapQueries();
+ _session.reapQueries();
try
{
@@ -106,7 +104,7 @@ class LibraryI extends _LibraryDisp
context.obtain();
BookQueryResultI impl = new BookQueryResultI(context, rs);
result.value = BookQueryResultPrxHelper.uncheckedCast(current.adapter.addWithUUID(impl));
- context.getSession().add(result.value, impl);
+ _session.add(result.value, impl);
}
}
catch(java.sql.SQLException e)
@@ -121,7 +119,7 @@ class LibraryI extends _LibraryDisp
createBook(String isbn, String title, java.util.List<String> authors, Ice.Current current)
throws BookExistsException
{
- SessionSQLRequestContext context = (SessionSQLRequestContext)SQLRequestContext.getCurrentContext();
+ SQLRequestContext context = SQLRequestContext.getCurrentContext();
assert context != null;
try
{
@@ -205,7 +203,10 @@ class LibraryI extends _LibraryDisp
}
}
- LibraryI()
+ LibraryI(SessionI session)
{
+ _session = session;
}
+
+ private SessionI _session;
}
diff --git a/java/demo/Database/library/SQLRequestContext.java b/java/demo/Database/library/SQLRequestContext.java
index 114c7a0558f..2b3050a9975 100644
--- a/java/demo/Database/library/SQLRequestContext.java
+++ b/java/demo/Database/library/SQLRequestContext.java
@@ -16,9 +16,9 @@
// 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,
+// When the request context is destroyed, the transaction is either
+// automatically committed or rolled back, depending whether the
+// request executed successfully.
//
class SQLRequestContext
{
@@ -31,6 +31,16 @@ class SQLRequestContext
}
}
+ public static void
+ initialize(Ice.Logger logger, ConnectionPool pool)
+ {
+ assert _logger == null;
+ assert _pool == null;
+
+ _logger = logger;
+ _pool = pool;
+ }
+
public java.sql.PreparedStatement
prepareStatement(String sql)
throws java.sql.SQLException
@@ -66,6 +76,7 @@ class SQLRequestContext
public void
destroy(boolean commit)
{
+ // Must only be called on an obtained context.
assert _obtain;
destroyInternal(commit);
}
@@ -80,11 +91,9 @@ class SQLRequestContext
_logger.error(prefix + ": error:\n" + sw.toString());
}
- SQLRequestContext(Ice.Logger logger, ConnectionPool pool)
+ SQLRequestContext()
{
- _logger = logger;
- _pool = pool;
- _conn = pool.acquire();
+ _conn = _pool.acquire();
synchronized(_contextMap)
{
@@ -105,11 +114,13 @@ class SQLRequestContext
{
synchronized(_contextMap)
{
- // Remove the current context from the map.
+ // Remove the current context from the thread->context
+ // map.
SQLRequestContext context = _contextMap.remove(Thread.currentThread());
assert context != null;
}
+ // If the context was obtained then don't destroy.
if(!_obtain)
{
destroyInternal(commit);
@@ -154,16 +165,16 @@ class SQLRequestContext
_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 static Ice.Logger _logger = null;
+ private static ConnectionPool _pool = null;
+
+ private boolean _trace = true;
private java.util.List<java.sql.Statement> _statements = new java.util.LinkedList<java.sql.Statement>();
private java.sql.Connection _conn;
private boolean _obtain = false;
diff --git a/java/demo/Database/library/Server.java b/java/demo/Database/library/Server.java
index b95bb76b56c..106ffa41df1 100644
--- a/java/demo/Database/library/Server.java
+++ b/java/demo/Database/library/Server.java
@@ -11,51 +11,8 @@ import Demo.*;
class LibraryServer extends Ice.Application
{
- static class BookDispatchInterceptorI extends Ice.DispatchInterceptor
- {
- BookDispatchInterceptorI(Ice.Logger logger, ConnectionPool pool, Ice.Object servant)
- {
- _logger = logger;
- _pool = pool;
- _servant = servant;
- }
-
- public Ice.DispatchStatus
- dispatch(Ice.Request request)
- {
- SQLRequestContext context = new SQLRequestContext(_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;
- }
-
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)
{
@@ -73,6 +30,11 @@ class LibraryServer extends Ice.Application
{
}
+ LocatorI(Ice.Object servant)
+ {
+ _servant = new DispatchInterceptorI(servant);
+ }
+
private Ice.Object _servant;
}
@@ -136,16 +98,13 @@ class LibraryServer extends Ice.Application
//
Ice.ObjectAdapter adapter = communicator().createObjectAdapter("SessionFactory");
- LocatorI locator = new LocatorI(logger, pool, new BookI());
- Ice.Object library = new LibraryI();
+ SQLRequestContext.initialize(logger, pool);
+ adapter.addServantLocator(new LocatorI(new BookI()), "book");
- adapter.add(new SessionFactoryI(logger, reaper, pool, library),
- communicator().stringToIdentity("SessionFactory"));
- adapter.add(new Glacier2SessionManagerI(logger, reaper, pool, library),
+ adapter.add(new SessionFactoryI(logger, reaper), communicator().stringToIdentity("SessionFactory"));
+ adapter.add(new Glacier2SessionManagerI(logger, reaper),
communicator().stringToIdentity("LibrarySessionManager"));
- adapter.addServantLocator(locator, "book");
-
//
// Everything ok, let's go.
//
diff --git a/java/demo/Database/library/SessionFactoryI.java b/java/demo/Database/library/SessionFactoryI.java
index 50957204ea9..e2cb487e716 100644
--- a/java/demo/Database/library/SessionFactoryI.java
+++ b/java/demo/Database/library/SessionFactoryI.java
@@ -14,25 +14,25 @@ class SessionFactoryI extends _SessionFactoryDisp
public synchronized SessionPrx
create(Ice.Current c)
{
- SessionI session = new SessionI(_logger, _pool, c.adapter, _libraryServant);
+ SessionI session = new SessionI(_logger, c.adapter);
_SessionTie servant = new _SessionTie(session);
+
SessionPrx proxy = SessionPrxHelper.uncheckedCast(c.adapter.addWithUUID(servant));
+
_logger.trace("SessionFactory", "create new session: " +
c.adapter.getCommunicator().identityToString(proxy.ice_getIdentity()));
+
_reaper.add(proxy, session);
+
return proxy;
}
- SessionFactoryI(Ice.Logger logger, ReapThread reaper, ConnectionPool pool, Ice.Object libraryServant)
+ SessionFactoryI(Ice.Logger logger, ReapThread reaper)
{
_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 bd460271e48..dc328e85369 100644
--- a/java/demo/Database/library/SessionI.java
+++ b/java/demo/Database/library/SessionI.java
@@ -11,46 +11,6 @@ import Demo.*;
class SessionI implements _SessionOperations, _Glacier2SessionOperations
{
- static class SessionDispatchInterceptorI extends Ice.DispatchInterceptor
- {
- SessionDispatchInterceptorI(Ice.Logger logger, ConnectionPool pool, Ice.Object servant, SessionI session)
- {
- _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
getLibrary(Ice.Current c)
{
@@ -164,13 +124,11 @@ class SessionI implements _SessionOperations, _Glacier2SessionOperations
}
}
- SessionI(Ice.Logger logger, ConnectionPool pool, Ice.ObjectAdapter adapter, Ice.Object libraryServant)
+ SessionI(Ice.Logger logger, Ice.ObjectAdapter adapter)
{
_logger = logger;
_timestamp = System.currentTimeMillis();
-
- _library = LibraryPrxHelper.uncheckedCast(
- adapter.addWithUUID(new SessionDispatchInterceptorI(logger, pool, libraryServant, this)));
+ _library = LibraryPrxHelper.uncheckedCast(adapter.addWithUUID(new DispatchInterceptorI(new LibraryI(this))));
}
static class QueryProxyPair
diff --git a/java/demo/Database/library/SessionSQLRequestContext.java b/java/demo/Database/library/SessionSQLRequestContext.java
deleted file mode 100644
index 799ce4a92f6..00000000000
--- a/java/demo/Database/library/SessionSQLRequestContext.java
+++ /dev/null
@@ -1,24 +0,0 @@
-// **********************************************************************
-//
-// 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;
-}
diff --git a/java/demo/Database/library/config.server b/java/demo/Database/library/config.server
index c8b743e9fb1..8d3ec99b6db 100644
--- a/java/demo/Database/library/config.server
+++ b/java/demo/Database/library/config.server
@@ -14,7 +14,7 @@ JDBC.Url=jdbc:mysql://localhost/library
JDBC.NumConnections=5
# Number of threads in the server-side dispatch thread pool.
-Ice.ThreadPool.Server.Size=1
+Ice.ThreadPool.Server.Size=5
#
# Warn about connection exceptions