Index: /main/trunk/greenstone3/build.properties.in
===================================================================
--- /main/trunk/greenstone3/build.properties.in	(revision 29902)
+++ /main/trunk/greenstone3/build.properties.in	(revision 29903)
@@ -15,4 +15,7 @@
 # uncomment the following if you have moved the greenstone3 web directory to tomcats webapps dir 
 #web.home=${tomcat.installed.path}/webapps/greenstone3
+
+# Default derby networked server portnumber is 1527, can change this here in case of conflict
+derby.server.port=1527
 
 ## Collection building options
Index: /main/trunk/greenstone3/build.xml
===================================================================
--- /main/trunk/greenstone3/build.xml	(revision 29902)
+++ /main/trunk/greenstone3/build.xml	(revision 29903)
@@ -606,8 +606,14 @@
     <pathelement location="${web.writablelib}"/>
     <fileset dir="${web.writablelib}">
-      <include name="derby.jar"/>
+      <include name="derbyclient.jar"/> <!--<include name="derby.jar"/>-->
     </fileset>
   </path>
   
+  <path id="derby.server.classpath">
+    <pathelement location="${web.lib}/derbynet.jar"/>
+    <pathelement location="${web.lib}/derby.jar"/>
+  </path>
+
+
   <path id="local.tomcat.path">
     <pathelement location="${basedir}/bin/script"/>
@@ -780,8 +786,38 @@
 
   <target name="get-default-servlet-url">
-	<echo>http://${tomcat.server}:${tomcat.port}${app.path}${server.default.servlet}</echo>
+    <echo>http://${tomcat.server}:${tomcat.port}${app.path}${server.default.servlet}</echo>
   </target>	
   
-  <target name="start" depends="init,configure-tomcat,configure-web,configure-solr-ext,start-tomcat"
+  <target name="start-derby-java">
+    <echo>Launching derby on ${derby.server.port}...</echo>
+    <java classname="org.apache.derby.drda.NetworkServerControl" fork="true" spawn="true" clonevm="true">      
+      <arg value="start"/>
+      <classpath refid="derby.server.classpath"/>
+    </java>
+  </target>
+  
+  <!-- Using derby 10.1.2.1
+       See db-derby-10.1.2.1-bin/docs/html/adminguide/index.html -->
+  <target name="start-derby">
+    <echo>About to launch derby on ${derby.server.port}</echo>
+    <property name="derby.server.classpath.prop" refid="derby.server.classpath" />
+
+    <exec executable="java" spawn="true"><!-- failonerror="true"-->
+      <env key="CLASSPATH" path="${derby.server.classpath.prop}"/>
+      <arg value="org.apache.derby.drda.NetworkServerControl"/>
+      <arg value="start"/>
+      <arg value="-p"/>
+      <arg value="${derby.server.port}"/>
+    </exec>   
+  </target>
+
+  <target name="stop-derby">
+    <java classname="org.apache.derby.drda.NetworkServerControl">
+      <arg value="shutdown"/>
+      <classpath refid="derby.server.classpath"/>
+    </java>
+  </target>
+
+  <target name="start" depends="init,configure-tomcat,configure-web,configure-solr-ext,start-derby,start-tomcat"
     description="Startup the Tomcat server." >
     <echo>${app.name} (${app.version}) server running using Apache Tomcat and Java</echo> 
@@ -813,5 +849,5 @@
   </target>
 
-  <target name="stop" depends="init,stop-tomcat" 
+  <target name="stop" depends="init,stop-tomcat,stop-derby" 
     description="Shutdown the Tomcat server."/>
   
@@ -1201,4 +1237,5 @@
     <filter token="tomcat.server" value="${tomcat.server}"/>
     <filter token="tomcat.port" value="${tomcat.port}"/>
+    <filter token="derbyserver.port" value="${derby.server.port}"/>
     <filter token="perlpath" value="${escaped.perl.path}"/>
     <filter token="disable.collection.building" value="${disable.collection.building}"/>
@@ -1352,4 +1389,5 @@
 	<filter token="privilegedattribute" value="${privileged.attribute}"/>
 	<filter token="allowedIPs" value="${allowed.IPs}"/>
+	<filter token="derbyserverport" value="${derby.server.port}"/>
       </filterset>
     </copy>
@@ -1735,7 +1773,11 @@
   <target name="update-userdb" description="Add or modify users" depends="check-tomcat-running">
 
-    <!-- stop tomcat if running, since derby db is embedded and only allows connections from one jvm instance at a time
+    <!-- 
+       We're now using derby networked server, so stopping and starting tomcat is not necessary.
+       For embedded derby: stop tomcat if running, since derby db is embedded 
+       and only allows connections from one jvm instance at a time
 	 See http://db.apache.org/derby/papers/DerbyTut/embedded_intro.html
 	 The ${tomcat.isrunning} property is set by the depends-target "check-tomcat-running" -->
+    <!--
     <if>
       <bool>
@@ -1744,4 +1786,5 @@
       <antcall target="stop-tomcat"/>
     </if>
+    -->
 
     <!--<echo>${admin.password}</echo>--> <!-- for testing -->
@@ -1758,4 +1801,5 @@
     
     <!-- run tomcat again if it used to be running -->
+    <!--
     <if>
       <bool>
@@ -1764,4 +1808,5 @@
       <antcall target="start-tomcat"/>
     </if>
+    -->
   </target>
 
Index: /main/trunk/greenstone3/resources/tomcat/greenstone3.xml.in
===================================================================
--- /main/trunk/greenstone3/resources/tomcat/greenstone3.xml.in	(revision 29902)
+++ /main/trunk/greenstone3/resources/tomcat/greenstone3.xml.in	(revision 29903)
@@ -10,8 +10,13 @@
 	allowLinking="false"
 	xmlBlockExternal="false">
-  
+
+<!--
+   For embedded derby db:
+   driverName="org.apache.derby.jdbc.EmbeddedDriver"
+   connectionURL="jdbc:derby:@gsdl3webhome@/etc/usersDB"
+-->  
 	<Realm className="org.apache.catalina.realm.JDBCRealm" 
-		driverName="org.apache.derby.jdbc.EmbeddedDriver"
-		connectionURL="jdbc:derby:@gsdl3webhome@/etc/usersDB"
+		driverName="org.apache.derby.jdbc.ClientDriver"
+		connectionURL="jdbc:derby://localhost:@derbyserverport@/@gsdl3webhome@/etc/usersDB"
 		userTable="users" userNameCol="username" userCredCol="password"
 		userRoleTable="roles" roleNameCol="role"
Index: /main/trunk/greenstone3/resources/web/global.properties.in
===================================================================
--- /main/trunk/greenstone3/resources/web/global.properties.in	(revision 29902)
+++ /main/trunk/greenstone3/resources/web/global.properties.in	(revision 29903)
@@ -19,4 +19,5 @@
 tomcat.port=@tomcat.port@
 tomcat.context=greenstone3
+derby.server.port=@derbyserver.port@
 
 ## Proxy setup - set these if you are behind a firewall and you want services that access the internet
Index: /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/sql/derby/DerbySQLServer.java
===================================================================
--- /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/sql/derby/DerbySQLServer.java	(revision 29902)
+++ /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/sql/derby/DerbySQLServer.java	(revision 29903)
@@ -8,7 +8,11 @@
 import java.sql.SQLException;
 
+import org.greenstone.util.GlobalProperties;
+
 public class DerbySQLServer implements SQLServer{
-    static final String PROTOCOL = "jdbc:derby:";
-    static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
+   
+    static final String PORT = GlobalProperties.getProperty("derby.server.port");//, "1527");
+    static final String PROTOCOL = "jdbc:derby://localhost:"+PORT+"/"; //"jdbc:derby:";
+    static final String DRIVER = "org.apache.derby.jdbc.ClientDriver"; //"org.apache.derby.jdbc.EmbeddedDriver";
     static Logger logger = Logger.getLogger(org.greenstone.gsdl3.sql.derby.DerbySQLServer.class.getName());     
 
@@ -51,6 +55,11 @@
     public boolean disconnect(String databasePath){
 	try{
-	    String protocol_str = PROTOCOL + databasePath + ";shutdown=true"; 
-	    DriverManager.getConnection(protocol_str);
+	    // Only shutdown if using embedded derby, 
+	    // not if it's a networked derby server, which is what we now use
+
+	    if(DRIVER.equals("org.apache.derby.jdbc.EmbeddedDriver")) {
+		String protocol_str = PROTOCOL + databasePath + ";shutdown=true"; 	    
+		DriverManager.getConnection(protocol_str);
+	    }
 	}catch (SQLException se){
 	    String theError = (se).getSQLState();
Index: /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/DerbyWrapper.java
===================================================================
--- /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/DerbyWrapper.java	(revision 29902)
+++ /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/DerbyWrapper.java	(revision 29903)
@@ -35,6 +35,7 @@
 public class DerbyWrapper
 {
-	static final String PROTOCOL = "jdbc:derby:";
-	static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
+    static final String PORT = GlobalProperties.getProperty("derby.server.port");//, "1527");
+    static final String PROTOCOL = "jdbc:derby://localhost:"+PORT+"/"; // "jdbc:derby:";
+    static final String DRIVER = "org.apache.derby.jdbc.ClientDriver"; //"org.apache.derby.jdbc.EmbeddedDriver";
 	static final String USERSDB = "usersDB";
 	static final String USERS = "users";
@@ -134,5 +135,9 @@
 		{
 			//  shutdown the whole server
+		    // but not if we're a derby client using the derby network server 
+		    // only if we're using an embedded derby
+		    if(DRIVER.equals("org.apache.derby.jdbc.EmbeddedDriver")) {
 			DriverManager.getConnection(PROTOCOL + ";shutdown=true");
+		    }
 		}
 		catch (SQLException se)
