summaryrefslogtreecommitdiff
path: root/java/demo/Database/library
diff options
context:
space:
mode:
Diffstat (limited to 'java/demo/Database/library')
-rw-r--r--java/demo/Database/library/BookI.java6
-rw-r--r--java/demo/Database/library/Client.java2
-rw-r--r--java/demo/Database/library/ConnectionPool.java8
-rw-r--r--java/demo/Database/library/Glacier2SessionManagerI.java3
-rw-r--r--java/demo/Database/library/Library.ice2
-rw-r--r--java/demo/Database/library/LibraryI.java20
-rw-r--r--java/demo/Database/library/Parser.java14
-rw-r--r--java/demo/Database/library/README77
-rw-r--r--java/demo/Database/library/ReapThread.java6
-rw-r--r--java/demo/Database/library/RunParser.java8
-rw-r--r--java/demo/Database/library/SQLRequestContext.java9
-rw-r--r--java/demo/Database/library/Server.java17
-rw-r--r--java/demo/Database/library/SessionFactoryI.java3
-rw-r--r--java/demo/Database/library/SessionI.java5
-rw-r--r--java/demo/Database/library/build.xml12
-rw-r--r--java/demo/Database/library/config.client8
-rw-r--r--java/demo/Database/library/config.server4
17 files changed, 122 insertions, 82 deletions
diff --git a/java/demo/Database/library/BookI.java b/java/demo/Database/library/BookI.java
index 306b04650c0..90173b8417d 100644
--- a/java/demo/Database/library/BookI.java
+++ b/java/demo/Database/library/BookI.java
@@ -9,8 +9,10 @@
import Demo.*;
-// This servant is a default servant. The book identity is retreived
-// from Ice.Current object.
+//
+// This servant is a default servant. The book identity is retrieved
+// from the Ice.Current object.
+//
class BookI extends _BookDisp
{
public void
diff --git a/java/demo/Database/library/Client.java b/java/demo/Database/library/Client.java
index 8a181ef1519..78d375c44f8 100644
--- a/java/demo/Database/library/Client.java
+++ b/java/demo/Database/library/Client.java
@@ -42,6 +42,6 @@ public class Client extends Ice.Application
main(String[] args)
{
Client app = new Client();
- app.main("demo.Freeze.library.Client", args, "config.client");
+ app.main("demo.Database.library.Client", args, "config.client");
}
}
diff --git a/java/demo/Database/library/ConnectionPool.java b/java/demo/Database/library/ConnectionPool.java
index 4e0b32f759a..dd1eff8ee61 100644
--- a/java/demo/Database/library/ConnectionPool.java
+++ b/java/demo/Database/library/ConnectionPool.java
@@ -21,7 +21,7 @@ class ConnectionPool
_nconnections = numConnections;
while(numConnections-- > 0)
{
- java.sql.Connection connection = java.sql.DriverManager.getConnection (url, username, password);
+ java.sql.Connection connection = java.sql.DriverManager.getConnection(url, username, password);
connection.setAutoCommit(false);
_connections.add(connection);
}
@@ -94,14 +94,14 @@ class ConnectionPool
conn = null;
}
- // If the connection has been closed, or is otherwise invalid
- // re-establish the connection.
+ // If the connection has been closed, or is otherwise invalid,
+ // we need to re-establish the connection.
while(conn == null)
{
_logger.trace("ConnectionPool", "establishing new database connection");
try
{
- conn = java.sql.DriverManager.getConnection (_url, _username, _password);
+ conn = java.sql.DriverManager.getConnection(_url, _username, _password);
conn.setAutoCommit(false);
}
catch(java.sql.SQLException e)
diff --git a/java/demo/Database/library/Glacier2SessionManagerI.java b/java/demo/Database/library/Glacier2SessionManagerI.java
index 8e36b88a681..1c3ba18faa0 100644
--- a/java/demo/Database/library/Glacier2SessionManagerI.java
+++ b/java/demo/Database/library/Glacier2SessionManagerI.java
@@ -17,7 +17,8 @@ class Glacier2SessionManagerI extends Glacier2._SessionManagerDisp
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: " + proxy.ice_getIdentity());
+ _logger.trace("SessionFactory", "create new session: " +
+ c.adapter.getCommunicator().identityToString(proxy.ice_getIdentity()));
_reaper.add(proxy, session);
return proxy;
}
diff --git a/java/demo/Database/library/Library.ice b/java/demo/Database/library/Library.ice
index 61a4fac5dcb..16c069e661c 100644
--- a/java/demo/Database/library/Library.ice
+++ b/java/demo/Database/library/Library.ice
@@ -199,7 +199,7 @@ interface Library
/**
*
* Query based on the author name. The query is a partial match of
- * the authors name.
+ * the author's name.
*
* @param author The authors name.
*
diff --git a/java/demo/Database/library/LibraryI.java b/java/demo/Database/library/LibraryI.java
index 6c2a4ddeeb8..7f01f9f87c6 100644
--- a/java/demo/Database/library/LibraryI.java
+++ b/java/demo/Database/library/LibraryI.java
@@ -9,9 +9,11 @@
import Demo.*;
-// This servant is a default servant. The book identity is retreived
-// from Ice.Current object. The session object associated with the
-// library is also retrieved using the Libraries identity.
+//
+// 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.
+//
class LibraryI extends _LibraryDisp
{
public void
@@ -77,7 +79,7 @@ class LibraryI extends _LibraryDisp
try
{
- // Find each of the authors.
+ // Find each of the authors.
java.sql.PreparedStatement stmt = context.prepareStatement("SELECT * FROM authors WHERE name LIKE ?");
stmt.setString(1, "%" + author + "%");
java.sql.ResultSet rs = stmt.executeQuery();
@@ -86,8 +88,7 @@ class LibraryI extends _LibraryDisp
throw new NoResultsException();
}
- // Build a query that finds all books by these set of
- // authors.
+ // Build a query that finds all books by these authors.
StringBuffer sb = new StringBuffer("SELECT * FROM books INNER JOIN authors_books ON " +
"books.id=authors_books.book_id AND (");
boolean front = true;
@@ -115,8 +116,7 @@ class LibraryI extends _LibraryDisp
first.value = BookI.extractDescription(context, rs, current.adapter);
if(rs.next())
{
- // The SQLRequestContext is now owned by the query
- // implementation.
+ // The SQLRequestContext is now owned by the query implementation.
context.obtain();
BookQueryResultI impl = new BookQueryResultI(_logger, context, rs);
result.value = BookQueryResultPrxHelper.uncheckedCast(current.adapter.addWithUUID(impl));
@@ -173,7 +173,7 @@ class LibraryI extends _LibraryDisp
{
// Otherwise, create a new author record.
stmt = context.prepareStatement("INSERT INTO authors (name) VALUES(?)",
- java.sql.Statement.RETURN_GENERATED_KEYS);
+ java.sql.Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, author);
int count = stmt.executeUpdate();
assert count == 1;
@@ -189,7 +189,7 @@ class LibraryI extends _LibraryDisp
// Create the new book.
stmt = context.prepareStatement("INSERT INTO books (isbn, title) VALUES(?, ?)",
- java.sql.Statement.RETURN_GENERATED_KEYS);
+ java.sql.Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, isbn);
stmt.setString(2, title);
int count = stmt.executeUpdate();
diff --git a/java/demo/Database/library/Parser.java b/java/demo/Database/library/Parser.java
index 2df81259696..2780cbb0dc8 100644
--- a/java/demo/Database/library/Parser.java
+++ b/java/demo/Database/library/Parser.java
@@ -37,10 +37,10 @@ class Parser
{
if(args.size() != 3)
{
- error("`add' requires at exactly three arguments (type `help' for more info)");
+ error("`add' requires exactly three arguments (type `help' for more info)");
return;
}
-
+
try
{
String isbn = (String)args.get(0);
@@ -52,7 +52,7 @@ class Parser
{
authors.add(st.nextToken().trim());
}
-
+
BookPrx book = _library.createBook(isbn, title, authors);
System.out.println("added new book with isbn " + isbn);
}
@@ -74,7 +74,7 @@ class Parser
error("`isbn' requires exactly one argument (type `help' for more info)");
return;
}
-
+
try
{
if(_query != null)
@@ -117,7 +117,7 @@ class Parser
error("`authors' requires exactly one argument (type `help' for more info)");
return;
}
-
+
try
{
if(_query != null)
@@ -172,7 +172,7 @@ class Parser
}
printCurrent();
}
-
+
void
printCurrent()
{
@@ -180,7 +180,7 @@ class Parser
{
if(_current != null)
{
- System.out.println("current book is:" );
+ System.out.println("current book is:");
System.out.println("isbn: " + _current.isbn);
System.out.println("title: " + _current.title);
System.out.println("authors: " + _current.authors);
diff --git a/java/demo/Database/library/README b/java/demo/Database/library/README
index 51b60cf23d7..8ff61f56f89 100644
--- a/java/demo/Database/library/README
+++ b/java/demo/Database/library/README
@@ -1,40 +1,67 @@
MySQL JDBC Demo
===============
-This demo shows how to implement an Ice server that uses mysql
-through a JDBC API.
+This demo shows how to implement an Ice server that uses mysql through
+a JDBC API and demonstrates the following techniques:
-It is a fairly simple demo that illustrates how to:
-
- - Map relational data to Ice objects, in particular convert between
- Ice and JDBC types.
- - Use an JDBC connection pool to provide JDBC connections to Ice
+ - Mapping relational data to Ice objects, and in particular the
+ conversion between Ice and JDBC types.
+ - Using a JDBC connection pool to provide JDBC connections for Ice
requests.
- - Use an Ice servant locator.
+ - Using an Ice servant locator.
Building the demo
-----------------
-- Install mysql.
+1. Install mysql if necessary.
+
+2. Download version 5.0.8 of the mysql JDBC connector here:
+
+ http://dev.mysql.com/downloads/connector/j/5.0.html
+
+ After extracting the archive, add mysql-connector-java-5.0.8-bin.jar
+ to your CLASSPATH.
+
+3. Create a database named "library" and grant privileges to a user. In
+ the commands below, replace USER with the name you have chosen and
+ PASSWORD with a suitable password:
+
+ $ mysql -u root -p
+ Enter password:
+ Welcome to the MySQL monitor.
+
+ mysql> CREATE DATABASE library;
+ Query OK, 1 row affected (0.00 sec)
+
+ mysql> GRANT ALL PRIVILEGES ON library.* TO "USER"@"localhost"
+ -> IDENTIFIED BY "PASSWORD";
+ Query OK, 0 rows affected (0.00 sec)
-- Download version 5.0.8 of the mysql JDBC connector. Ensure that
- mysql-connector-java-5.0.8-bin.jar is in your CLASSPATH.
+ mysql> FLUSH PRIVILEGES;
+ Query OK, 0 rows affected (0.01 sec)
-- Ensure that your user (by default, matthew) has appropriate
- privileges.
+ mysql> EXIT
-- Create the SQL tables using the provided createTypes.sql
- script. For example:
+4. Create the SQL tables using the script createTypes.sql:
+
+ $ mysql --user=USER --pass=PASSWORD library < createTypes.sql
+
+5. Edit the JDBC properties in config.server to reflect your selected
+ user name and password:
+
+ JDBC.Username=USER
+ JDBC.Password=PASSWORD
+
+NOTE: The instructions assume that the demo server runs on the same
+ host as the mysql server. If you intend to run the demo server on
+ a different host than the mysql server, you will need to revise
+ the mysql privileges as well as the JDBC URL in config.server.
- $ mysql --user=matthew --pass=foo library < initialize.mysql
Running the demo
----------------
-Review the JDBC properties in the config.server file. You may need to
-change them to connect to your mysql server.
-
To run the demo, first start the server:
$ java Server
@@ -50,16 +77,18 @@ $ java Client
Type "help" to get a list of valid commands.
-The demo also supports a Glacier2 deployment. To do so, edit
-config.client and uncomment the three configuration parameters:
+
+Running the demo with Glacier2
+------------------------------
+
+The demo also supports a Glacier2 deployment. You will need to edit
+config.client and uncomment these configuration parameters:
#Ice.Default.Router=DemoGlacier2/router:ssl -p 4064 -h 127.0.0.1
#Ice.ACM.Client=0
#Ice.RetryIntervals=-1
-Then to run the demo:
-
-Start the server:
+To run the demo using Glacier2, first start the server:
$ java Server
diff --git a/java/demo/Database/library/ReapThread.java b/java/demo/Database/library/ReapThread.java
index 0735f3d243e..54b50a6a907 100644
--- a/java/demo/Database/library/ReapThread.java
+++ b/java/demo/Database/library/ReapThread.java
@@ -10,7 +10,7 @@
import Demo.*;
class ReapThread extends Thread
-{
+{
static class SessionProxyPair
{
SessionProxyPair(Demo.SessionPrx p, SessionI s)
@@ -49,7 +49,7 @@ class ReapThread extends Thread
catch(InterruptedException e)
{
}
-
+
if(!_terminated)
{
java.util.Iterator<SessionProxyPair> p = _sessions.iterator();
@@ -95,7 +95,7 @@ class ReapThread extends Thread
// Destroy each of the sessions, releasing any resources they
// may hold. This calls directly on the session, not via the
// proxy since terminate() is called after the communicator is
- // shutdown, which means calls on collocated objects is not
+ // shutdown, which means calls on collocated objects are not
// permitted.
java.util.Iterator<SessionProxyPair> p = _sessions.iterator();
while(p.hasNext())
diff --git a/java/demo/Database/library/RunParser.java b/java/demo/Database/library/RunParser.java
index 93e31cc2206..e2d7798f385 100644
--- a/java/demo/Database/library/RunParser.java
+++ b/java/demo/Database/library/RunParser.java
@@ -27,11 +27,12 @@ class RunParser
{
return _session.getLibrary();
}
+
public void destroy()
{
_session.destroy();
}
-
+
public void refresh()
{
_session.refresh();
@@ -51,11 +52,12 @@ class RunParser
{
return _session.getLibrary();
}
+
public void destroy()
{
_session.destroy();
}
-
+
public void refresh()
{
_session.refresh();
@@ -173,7 +175,7 @@ class RunParser
System.err.println(appName + ": invalid object reference");
return 1;
}
-
+
session = new DemoSessionAdapter(factory.create());
}
SessionRefreshThread refresh = new SessionRefreshThread(communicator.getLogger(), 5000, session);
diff --git a/java/demo/Database/library/SQLRequestContext.java b/java/demo/Database/library/SQLRequestContext.java
index 226f8d50001..34274ecf2a9 100644
--- a/java/demo/Database/library/SQLRequestContext.java
+++ b/java/demo/Database/library/SQLRequestContext.java
@@ -7,8 +7,9 @@
//
// **********************************************************************
+//
// A SQL request context encapsulates SQL resources allocated in the
-// process a of executing a request, such as the database connection,
+// 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
@@ -18,6 +19,7 @@
// 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
@@ -100,7 +102,7 @@ class SQLRequestContext
}
_pool.release(_conn);
-
+
_statements.clear();
_conn = null;
_pool = null;
@@ -152,7 +154,8 @@ class SQLRequestContext
}
// A map of threads to request contexts.
- private static java.util.Map<Thread, SQLRequestContext> _contextMap = new java.util.HashMap<Thread, SQLRequestContext>();
+ private static java.util.Map<Thread, SQLRequestContext> _contextMap =
+ new java.util.HashMap<Thread, SQLRequestContext>();
private Ice.Logger _logger;
private ConnectionPool _pool;
diff --git a/java/demo/Database/library/Server.java b/java/demo/Database/library/Server.java
index d1043988a55..8421b277e44 100644
--- a/java/demo/Database/library/Server.java
+++ b/java/demo/Database/library/Server.java
@@ -40,8 +40,8 @@ class LibraryServer extends Ice.Application
}
public void
- finished(Ice.Current c, Ice.Object servant, Object cookie)
- {
+ 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).
@@ -53,9 +53,9 @@ class LibraryServer extends Ice.Application
}
public void
- deactivate(String category)
- {
- }
+ deactivate(String category)
+ {
+ }
private Ice.Logger _logger;
private ConnectionPool _pool;
@@ -88,7 +88,7 @@ class LibraryServer extends Ice.Application
try
{
- Class.forName ("com.mysql.jdbc.Driver").newInstance ();
+ Class.forName ("com.mysql.jdbc.Driver").newInstance();
}
catch(Exception e)
{
@@ -116,7 +116,7 @@ class LibraryServer extends Ice.Application
ReapThread reaper = new ReapThread(logger);
reaper.start();
-
+
//
// Create an object adapter
//
@@ -125,7 +125,8 @@ class LibraryServer extends Ice.Application
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), communicator().stringToIdentity("LibrarySessionManager"));
+ adapter.add(new Glacier2SessionManagerI(logger, reaper),
+ communicator().stringToIdentity("LibrarySessionManager"));
adapter.addServantLocator(locator, "book");
adapter.addServantLocator(locator, "library");
diff --git a/java/demo/Database/library/SessionFactoryI.java b/java/demo/Database/library/SessionFactoryI.java
index f8463b5cdb6..cb365b576a7 100644
--- a/java/demo/Database/library/SessionFactoryI.java
+++ b/java/demo/Database/library/SessionFactoryI.java
@@ -17,7 +17,8 @@ class SessionFactoryI extends _SessionFactoryDisp
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: " + proxy.ice_getIdentity());
+ _logger.trace("SessionFactory", "create new session: " +
+ c.adapter.getCommunicator().identityToString(proxy.ice_getIdentity()));
_reaper.add(proxy, session);
return proxy;
}
diff --git a/java/demo/Database/library/SessionI.java b/java/demo/Database/library/SessionI.java
index f5bc36702d4..e7cc744ed80 100644
--- a/java/demo/Database/library/SessionI.java
+++ b/java/demo/Database/library/SessionI.java
@@ -49,7 +49,8 @@ class SessionI implements _SessionOperations, _Glacier2SessionOperations
}
_destroyed = true;
- _logger.trace("Session", "session " + c.id + " is now destroyed.");
+ _logger.trace("Session", "session " + c.adapter.getCommunicator().identityToString(c.id) +
+ " is now destroyed.");
// Remove the session from the sessions map.
synchronized(_sessions)
@@ -165,7 +166,7 @@ class SessionI implements _SessionOperations, _Glacier2SessionOperations
BookQueryResultPrx proxy;
BookQueryResultI impl;
- };
+ }
private static java.util.Map<Ice.Identity, SessionI> _sessions =
new java.util.HashMap<Ice.Identity, SessionI>();
diff --git a/java/demo/Database/library/build.xml b/java/demo/Database/library/build.xml
index 5497ed50a9e..49b2ee2269a 100644
--- a/java/demo/Database/library/build.xml
+++ b/java/demo/Database/library/build.xml
@@ -9,7 +9,7 @@
**********************************************************************
-->
-<project name="demo_Freeze_library" default="all" basedir=".">
+<project name="demo_Database_library" default="all" basedir=".">
<!-- set global properties for this build -->
<property name="top.dir" value="../../.."/>
@@ -21,7 +21,7 @@
<!-- Create the output directory for generated code -->
<mkdir dir="${generated.dir}"/>
<slice2java outputdir="${generated.dir}" tie="on">
- <meta value="${java2metadata}"/>
+ <meta value="${java2metadata}"/>
<includepath>
<pathelement path="${slice.dir}" />
</includepath>
@@ -34,14 +34,14 @@
<target name="compile" depends="generate">
<mkdir dir="${class.dir}"/>
<javac srcdir="${generated.dir}" destdir="${class.dir}"
- debug="${debug}">
+ debug="${debug}">
<classpath refid="ice.classpath"/>
- <compilerarg value="${javac.lint}"/>
+ <compilerarg value="${javac.lint}"/>
</javac>
- <javac srcdir="." destdir="${class.dir}"
+ <javac srcdir="." destdir="${class.dir}"
excludes="generated/**" debug="${debug}">
<classpath refid="ice.classpath"/>
- <compilerarg value="${javac.lint}"/>
+ <compilerarg value="${javac.lint}"/>
</javac>
</target>
diff --git a/java/demo/Database/library/config.client b/java/demo/Database/library/config.client
index 7041c592de8..383332c2630 100644
--- a/java/demo/Database/library/config.client
+++ b/java/demo/Database/library/config.client
@@ -2,25 +2,25 @@
# The client reads this property to create the reference to the
# "SessionFactory" object in the server.
#
-#SessionFactory.Proxy=SessionFactory:default -p 10000
+SessionFactory.Proxy=SessionFactory:default -p 10000
#
# The proxy to the Glacier2 router for all outgoing connections. This
# must match the value of Glacier2.Client.Endpoints in config.glacier2.
#
-Ice.Default.Router=DemoGlacier2/router:ssl -p 4064 -h 127.0.0.1
+#Ice.Default.Router=DemoGlacier2/router:ssl -p 4064 -h 127.0.0.1
#
# No active connection management is permitted with Glacier2.
# Connections must remain established.
#
-Ice.ACM.Client=0
+#Ice.ACM.Client=0
#
# Connection retry is not possible with Glacier2. Connections must
# remain established.
#
-Ice.RetryIntervals=-1
+#Ice.RetryIntervals=-1
#
# Warn about connection exceptions
diff --git a/java/demo/Database/library/config.server b/java/demo/Database/library/config.server
index 4cbefb70ffc..c8b743e9fb1 100644
--- a/java/demo/Database/library/config.server
+++ b/java/demo/Database/library/config.server
@@ -4,8 +4,8 @@
SessionFactory.Endpoints=default -p 10000
# JDBC configuration.
-JDBC.Username=matthew
-JDBC.Password=foo
+JDBC.Username=USER
+JDBC.Password=PASSWORD
JDBC.Url=jdbc:mysql://localhost/library
# The number of connections in the JDBC connection pool. This number