Flavio Palumbo wrote:
Hi all,
I wrote a little class, derived from NsSample.java found in
D:\db-derby-10.1.2.1-bin\demo\nserverdemo,
to start an embedded server and use it with an embedded client (no embedded
driver but org.apache.derby.jdbc.ClientDriver) fron within a java application,
all in the same JVM.
The first strange thing I found is that if I try to access the data with
the ij utility while the application is running, I receive a messagge like
this :
ERROR XJ040: Failed to start database 'c:\ESQueryProd\derbyDB', see the next
exception for details.
ERROR XSDB6: Another instance of Derby may have already booted the database
C:\ESQueryProd\derbyDB.
Obviously, when I close the application everything goes fine.
I thought that network server shares connection and data, but it seems not
to be true.
How can I do that ?
Hello Flavio,
If I understand you correctly, what you are trying to do should be
possible. To make sure we are on the same page here, I have written a
small application that creates two connections to a database:
1) I instruct Derby to start the NetworkServer by setting the property
derby.drda.startNetworkServer = true.
2) The first connection creates the database 'testDB' using the
embedded driver. The network server is also started now.
3) The second connection to 'testDB' is created by using the client
driver.
4) I can connect to the same database using ij by specifying the
following command (from ij):
> connect 'jdbc:derby://localhost/testDB';
Does this demonstrate what you are trying to to?
You need to have the Derby jars in your classpath when running the
application (and ij). Which exactly depends on your Derby version.
I think there are variations on how to do this, and I'm sure people will
let us know if there are easier/better ways to do it.
If you have further questions, we'll try to answer them :)
Regards,
--
Kristian
I found the same behavior starting the application twice from newtwork :
the first application starts correctly, the second freezes until I shut down
the first.
Thanks for any suggetion
Flavio
PS I run with jvm 1.4.2_09 with win XP sp2 and derby 10.1.2.1
import java.sql.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class DerbyServerInApp {
public static void main(String[] args)
throws Exception {
// Inform Derby to start a networkserver.
System.setProperty("derby.drda.startNetworkServer", "true");
// Load the drivers. You can get away with only one of them, byt we
// use both here for demonstration.
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Class.forName("org.apache.derby.jdbc.ClientDriver");
// Connect and create a database.
Connection con = DriverManager.getConnection(
"jdbc:derby:testDB;create=true");
// Make sure the database is up before we connect with the
// ClientDriver.
try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException ie) {}
// Just loop and execute some queries.
QueryThread qt1 = new QueryThread(con, 5000);
QueryThread qt2 = new QueryThread(
DriverManager.getConnection("jdbc:derby://localhost/testDB"),
1000);
// Wait until user press ENTER
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println(">>>>> PRESS ENTER TO STOP APPLICATION");
qt1.start();
qt2.start();
in.readLine();
System.out.println("\n>>>>> Please wait a little while :)");
qt1.stop();
qt2.stop();
}
} // End class DerbyServerInApp
class QueryThread
implements Runnable {
private static int globalId = 0;
private final Connection con;
private final long interval;
private final int id;
private Thread thread = null;
private volatile boolean doRun = false;
public QueryThread(Connection con, long interval) {
this.con = con;
this.interval = interval;
this.id = ++globalId;
}
public void run() {
PreparedStatement ps;
try {
ps = con.prepareStatement("values (" + id + ", CURRENT_TIME)");
} catch (SQLException sqle) {
sqle.printStackTrace(System.out);
stop();
return;
}
ResultSet rs;
while (doRun) {
try {
rs = ps.executeQuery();
rs.next();
System.out.println(rs.getInt(1) + " :: " +
rs.getTime(2).toString());
rs.close();
} catch (SQLException sqle) {
sqle.printStackTrace(System.out);
stop();
}
try {
if (thread != null) {
thread.sleep(interval);
}
} catch (InterruptedException ie) {}
}
// Close the connection.
try {
con.close();
} catch (SQLException sqle) {
sqle.printStackTrace(System.out);
}
thread = null;
}
public synchronized void start() {
if (thread == null) {
doRun = true;
thread = new Thread(this);
thread.start();
}
}
public synchronized void stop() {
doRun = false;
}
} // End class QueryThread