Simple multiuser java database

R

Robert

I have written a no frills console math app that I would like to run
from multiple computers. I need help in replacing two methods that
store and retrieve data. I am currently using an array for
writing/reading strings but I need to replace the array with a
database. For various reasons I don't want to get wrapped up in
configuring and managing a SQL database but need to distribute this
classs to multiple computers. I'd like to find a small efficient
database that doesn't require the complexity of jdbc calls from my app
but will allow tcp calls for remote instances.

Robert
 
P

Paul Lutus

Robert said:
I have written a no frills console math app that I would like to run
from multiple computers. I need help in replacing two methods that
store and retrieve data. I am currently using an array for
writing/reading strings but I need to replace the array with a
database. For various reasons I don't want to get wrapped up in
configuring and managing a SQL database but need to distribute this
classs to multiple computers. I'd like to find a small efficient
database that doesn't require the complexity of jdbc calls from my app
but will allow tcp calls for remote instances.

If you want a database that is accessible by way of TCP calls, the next
questions are whether you intend to write it yourself, whether it will be
written in java, and whether the clients will read only or possibly modify
the database.

If the database requirement can be met by simply retrieving flat-file
records and no interactive writing will be done, this is very simple. If
you have to retrieve many records, or retrieve using any database-specific
methods such as SQL queries, or write to the database, this becomes much
more complex.

So, in summary, you need to define the problem more clearly. Start by moving
beyond saying what it is not.
 
J

John Davison

Robert said:
I have written a no frills console math app that I would like to run
from multiple computers. I need help in replacing two methods that
store and retrieve data. I am currently using an array for
writing/reading strings but I need to replace the array with a
database. For various reasons I don't want to get wrapped up in
configuring and managing a SQL database but need to distribute this
classs to multiple computers. I'd like to find a small efficient
database that doesn't require the complexity of jdbc calls from my app
but will allow tcp calls for remote instances.

Robert

Go here: http://freshmeat.net/browse/67/

(or navigate Freshmeat.net->Browse->Database->Database Engines/Servers)

- john
 
W

Will Hartung

Robert said:
I have written a no frills console math app that I would like to run
from multiple computers. I need help in replacing two methods that
store and retrieve data. I am currently using an array for
writing/reading strings but I need to replace the array with a
database. For various reasons I don't want to get wrapped up in
configuring and managing a SQL database but need to distribute this
classs to multiple computers. I'd like to find a small efficient
database that doesn't require the complexity of jdbc calls from my app
but will allow tcp calls for remote instances.

Since databases are systems pretty much specifically designed to handle
multiple users communicating over TCP (notably in details like user
contention, etc), it is likely that the added complexity of JDBC in your app
is lower than the complexity of a non-mainstream, less common database
solution.

JDBC is more verbose than complicated.

HSQL is a popular java database, IBM just released Cloudscape (under a new
name that I don't recall) to Apache, that is yet another Java database.

And then there are the popular MySQL, PostgreSQL, and Firebird.

RDBMSs are cheap, efficient, and ubiquitous today. Save for specialty
embedded applications, it's worth the time to embrace this technology as it
is quite handy and versatile, and not overly complicated to set up and
maintain today.

If a remote filesystem can't solve your problem easily, I'd make the leap to
a database, IMHO.

Regards,

Will Hartung
([email protected])
 
T

Thomas G. Marshall

Robert coughed up:
I have written a no frills console math app that I would like to run
from multiple computers. I need help in replacing two methods that
store and retrieve data. I am currently using an array for
writing/reading strings but I need to replace the array with a
database. For various reasons I don't want to get wrapped up in
configuring and managing a SQL database but need to distribute this
classs to multiple computers. I'd like to find a small efficient
database that doesn't require the complexity of jdbc calls from my app
but will allow tcp calls for remote instances.

Robert

If it's ok with you, please follow up with the solution you decide upon, and
what you've discovered along the way.

I'd like to know more about what you may figure out.
 
R

Robert

Paul Lutus said:
If you want a database that is accessible by way of TCP calls, the next
questions are whether you intend to write it yourself, whether it will be
written in java, and whether the clients will read only or possibly modify
the database.

If the database requirement can be met by simply retrieving flat-file
records and no interactive writing will be done, this is very simple. If
you have to retrieve many records, or retrieve using any database-specific
methods such as SQL queries, or write to the database, this becomes much
more complex.

So, in summary, you need to define the problem more clearly. Start by moving
beyond saying what it is not.


Paul,

As I mentioned above, the application reads and writes and requires
multiuser capability which is why I refer to the solution as a
database. The app is a number sieve so I'd like to keep it small and
efficient.

The current array read write process is very simple. The first loop
generates character strings which are appended to an array. At the
end of the loop, I call the read method whichs starts at the top of
the array, reads a line and flags it read and passes that value back
to the loop which may generate more data which gets sent to the write
method. The write method starts at the top of the array and writes
over any record flagged as read or gets appended to the bottom. The
order is not important since all records must be processed.

I am looking for a class with simple methods that write to a remote
file or database so I can replace the single user array I'm using now.
I do not want to use jdbc. The data source can be another class, an
embedded database such as Berkely or even an executable as long as it
is easy to configure and doesn't require the investment of time that a
sql server would.

I'm open to someone writing this for me if it doesn't cost too much
considering this is just a hobby.
 
S

Sudsy

Robert wrote:
I am looking for a class with simple methods that write to a remote
file or database so I can replace the single user array I'm using now.
I do not want to use jdbc. The data source can be another class, an
embedded database such as Berkely or even an executable as long as it
is easy to configure and doesn't require the investment of time that a
sql server would.

I'm open to someone writing this for me if it doesn't cost too much
considering this is just a hobby.

Depending on your target platform, you could write a small C program
which could use ndbm as the datastore. I'd do that but I already have
RDBMSs coming out of my ears... ;-)
(One of the many nice things about Linux)
 
W

Will Hartung

Robert said:
The current array read write process is very simple. The first loop
generates character strings which are appended to an array. At the
end of the loop, I call the read method whichs starts at the top of
the array, reads a line and flags it read and passes that value back
to the loop which may generate more data which gets sent to the write
method. The write method starts at the top of the array and writes
over any record flagged as read or gets appended to the bottom. The
order is not important since all records must be processed.

Sounds like a distributed queue.
I am looking for a class with simple methods that write to a remote
file or database so I can replace the single user array I'm using now.
I do not want to use jdbc. The data source can be another class, an
embedded database such as Berkely or even an executable as long as it
is easy to configure and doesn't require the investment of time that a
sql server would.

Here's the simplest solution I could think of:

It's a brain dead Servlet that takes its commands from the URL.

It has 3 commands:
add
next
reset

Add takes the argument and adds it to its internal queue.

Next returns the next item from the queue. If the queue is empty, it returns
the String "*EOL*".

Reset zeroes the queue.

The list is kept in memory within the Servlet container, and you lose all
your data when you reset the server.

The calls are synchronized, so there shouldn't be any race conditions.

---------
package pkg;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Server extends HttpServlet
{
LinkedList queue = new LinkedList();

public void init()
{
System.out.println("Servlet init.");
}

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
String next = request.getParameter("next");
String add = request.getParameter("add");
String reset = request.getParameter("reset");
if (next != null) {
Object top = "*EOL*";
synchronized(this) {
if (queue.size() > 0) {
top = queue.removeFirst();
}
}
response.getWriter().println(top);
}
if (add != null) {
synchronized(this) {
queue.add(add);
}
response.getWriter().println("OK");
}
if (reset != null) {
synchronized(this) {
queue = new LinkedList();
}
response.getWriter().println("OK");
}
}
}

And here's a simple test client.

package pkg;

import java.net.*;
import java.io.*;

public class TestClient
{
public static String host = "localhost";
public static int port = 8080;
public static String baseURL = "/server/items";

public static void main(String args[])
throws Exception
{
for (int i = 0; i < 50; i++) {
addItem("" + i);
}

// Get more than we put in to demonstrate *EOL*
for (int i = 0; i < 53; i++) {
System.out.println(getItem());
}
}

public static void addItem(String item)
throws Exception
{
String encodedItem = URLEncoder.encode(item, "UTF-8");
URL url = new URL("http", host, port,
baseURL + "?add=" + encodedItem);

Object o = url.getContent();
}

public static String getItem()
throws Exception
{
URL url = new URL("http", host, port,
baseURL + "?next");
BufferedReader br = new BufferedReader(new
InputStreamReader(url.openStream()));
String s = br.readLine();
return s;
}
}

Now, as you can imagine, this was hacked out very quickly.

What are the downsides of this technique? Well, for one, it probably tears
the socket up and down for each call to the server, but playing with the
URLConnection, fiddling with Keep-Alive and reusing a single connection
directly may well eliminate that.

It's a bit excessive for sending a string back and forth in terms of
overhead. That's the price you pay for an application specific protocol
layered on top of a general purpose protocol.

However, it is brain dead to set up. Get a binary install of Tomcat, and
plop it anyplace convenient on most any machine on the planet. You can email
me and I'll send you a zip of the entire build tree (4 files -- build.xml,
web.xml, Server.java, TestClient.java). Install Ant to build it.

And if your compute time is your bottleneck, this technique is essentially
"free".

It's also as customizable as you want it to be. You get to run down the
slippery slope of creating your own application protocol.

You can add persistence if you like, whatever. Read up on the Servlet spec
to fill in the blanks.

All said, I think JDBC and a RDBMS is a better solution, but if all you need
is what you specified, then this will do just that.

Like I said, if you want the build tree for the webapp, then you need just
add Tomcat and Ant and you're on your way to server bliss.

Good Luck!

Regards,

Will Hartung
([email protected])
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top