Skip to main content

bernt's Blog

Java DB 10.2 beta is now available for download. Try it out!

Posted by bernt on August 12, 2006 at 12:47 PM PDT

Java DB (or Derby, or Cloudscape if you prefer) 10.2 beta is available for download at http://people.apache.org/~rhillegas/10.2.1.0-beta/.

There has not been a feature release since august 2005, so this should be what Java DB/Derby users are waiting for. New features include:

  • Scrollable Updatable Result Sets
  • JDBC4
  • Grant/Revoke
  • Online Backup
  • Stronger Network Authentication

More information at http://wiki.apache.org/db-derby/TenTwoRelease. We in the developer community will appreciate feedback on very much. Try it out!

Related Topics >>

Do you need to learn how to use transactions? Again?

Posted by bernt on June 6, 2006 at 6:34 AM PDT

Statement: A lot of programmers do not know how to use transactions.
You may now think that I miss the target completely, but think for a while.

Very often they (and myself, I admit) think of transactions as atomic
changes to the database and write code like

... some SQL statements
commit
catch an SQLException and report a bug

This will work for most databases in most cases. BUT:

Seldom you see code that assumes that the transaction actually may
fail, and that it is normal for transactions to fail (i.e. it is not a
bug).

Occasionally, transactions do fail, and the frequency of failure is
dependent on the database and the applications using the
database. Transactions in distributed HA databases fail more
frequently than in simple databases, and I would assume distributed transactions (XA) also fail more often.
The proper way to write code for this is

while(not done) {
   ... some statements
   commit;
   done = true;
}  catch (SQLException e) {
   if (transient error) {
      ..
      rollback;
  } else {
      rollback;
      done = true;
      e.g. throw exception
  }
}

How often do you write like this?

Open Java DB

Posted by bernt on December 13, 2005 at 12:34 PM PST

Today at ApacheCon in San
Diego, Sun's Tim Bray said that href="http://db.apache.org/derby">Apache Derby will be distributed
by Sun as the open Java DB.

Java DB (or Derby) will appear in several Sun products and
NetBeans IDE 5.0 will have a Derby (or Java DB) plugin.

So, now we're going to have IBM's Cloudscape vs. Sun's Java DB
vs. Apache Derby. Confusing? Chaotic? Not really. The three are really
the same piece of software. It's just a matter of branding, packagin
and support.

And as a developer I really don't care that much as long as I'm able
to work with great people in the Derby community and make a great
product: "What's in a name? That which we call a rose
By any other word would smell as sweet." - Shakespeare

Press release

Related Topics >>

Apache Derby 10.1.2.1 Released

Posted by bernt on November 24, 2005 at 1:45 AM PST

Apache Derby, the pure Java
SQL database, has released version href="http://db.apache.org/derby/releases/release-10.1.2.1.cgi">10.1.2.1.

The changes since official release 10.1.1.0 include:

  • Globalization of the 10.1 error messages.
  • Eclipse 3.1 support
  • Ability to run on Mac OS X with no special configuration
  • Support for direct execution of derbynet.jar using the -jar option of the VM
  • Fixes to allow databases to be loaded from jars in the classpath
  • Fixes to improve J2ME/CDC/Foundation Profile support
  • Fixes to improve security manager support
  • Many important fixes to correct issues with data integrity, correct memory leaks and address other product defects

Release notes

Related Topics >>

Database benchmarks

Posted by bernt on October 14, 2005 at 2:03 AM PDT

In response to my A Read-only database in a jar? one of the commenters remarked that "Derby is alright but is much slower than HSQL" and referred to a benchmark at http://jamie.ideasasylum.com/notebook/index.php?id=4.

And guess what? He's is right. The problem is to figure out how he's right. I had one of our performance engineers look at the benchmark source and he came back telling me that this benchmark basically measures how fast the application connects to the databases and how fast they compile SQL.

And that's great. As long as your concern is connect times and SQL compilation. But if this benchmak was rewritten to be more economic with connects (e.g. with connection pooling) and to use prepared statements more systematically, the numbers might change dramatically.

It's easy to refer to a benchmark, but it's not always easy to communicate what that really means. The statement about "Derby is much slower than HSQL" is of limited value to you outside the context of

  1. what the referred benchmark measures and
  2. what your requirements are.

If you go to TPC and read about the various benchmarks (and these benchmarks are the real serious stuff, not some homegrown performance tests called "benchmarks" and put on the web), you'll soon realize that there are no such thing as a slower or faster database. They are only slower or faster given a specific set of criteria in a given benchmark.

In the end, a database that some benchmark labels as slow may just be what you need. If it performs fast enough and have the right functionality. Similarly, I don't buy the fastest car in the market. I don't even buy the fastest car I can afford. I buy a car I can afford that fits my needs... e.g. drive to the northern Norway with 2 grown-ups, 3 kids, a kayak, sleeping bags, a large tent, glacier climbing equipment, food, clothes and so on. Can't do that with a Lamborghini.

Related Topics >>

A Read-only database in a jar?

Posted by bernt on October 3, 2005 at 2:26 PM PDT

In Norway, all wines and liquors and beers stronger than 4.75 volume percent are sold at Vinmonopolet a state monopoly (Literally "The Wine Monopoly").

What has this to do with a read only database? Well, I once had an application on my Palm which contained Vinmonopolet's complete catalogue. Every now and then I updated the catalogue and was the able to search for information about their products (they carry a very impressive selection of good wines). The querying possibilities was somewhat limited. E.g. I could only search for wines within certain fixed price ranges, search for wines from only one country/district/area etc.

What I really wanted was an application with poweful searching possibilities: E.g. Show all Pinotage wines from South Africa and all Shiraz wines from Australia where the price is between NOK 130 and 150. Since I'm a programmer this power could be given me by the means of SQL. Yes, I admit that it would not be very user-friendly for the average wine-consumer, but one could imagine SQL queries as an "advanced option".

Ok. To the point. This application could be implemented as a small Java application on top of a SQL database. And yes, it's easy to make such an application with Apache Derby.

The follwing list shows roughly the steps needed to implement my favourite wine application:

  1. Create an application that populates a database for the intended purpose. Remember to do a proper shutdown of this database. This ensures that the database is checkpointed (see the Derby Reference Manual under the shutdown=true url attribute).
  2. Create the user application (of course..... ;-)
  3. Pack the database files and the user application into one jar file. The Derby property derby.storage.tempDirectory needs to be set, and the application need to do the above described shutdown when it is exited (Since a read-only database can be used from several Derby instances simultaneously, eavch instance will create a unique directory under the tempDirectory which will be deleted during shutdown). The database is accessed with the url jdbc:derby:classpath:<path to database>
  4. Ship your combined application/database jar file together with derby.jar and there you go.

Easy, isn't it? And I can envision a lot of other applications for a read only database. Can you?

2005-10-04 11:20:00 CEST: jwenting's comment is kind of right, so I changed from "read only database" to "read only database in a jar" in title and blurb to make the blog entry more precise.

Related Topics >>

Comments on "Easy String Concatenation Considered Harmful?"

Posted by bernt on September 28, 2005 at 2:15 PM PDT

I received some interesting comments on Easy String Concatenation Considered Harmful? and want to comment some of them.

I too love the easy string concatenation of Java. But ease of use and the readable code that comes from it will very often hinder the programmer from seeing the problem when it occurs.

I also agree that performance optimization should not be a developers first concern. But my article was written from my experience that when programmers encounters performance problems, lack of prepared statements (and their re-use) often is the problem.

And using persistence layers isn't always the cure, since I've seen auto-generated code with the same flaw. And then, you can't just get around it easily without either choosing another persistence layer or fixing the the persistence layer.

And, yes, "select * from...." is bad practice and due to my laziness when composing the article.

Related Topics >>

Easy String Concatenation Considered Harmful?

Posted by bernt on September 20, 2005 at 5:33 AM PDT

This is not about the amount of String objects generated when you do
string concatenation in Java. This is about databases and JDBC.

Performance problem?

When encountering performance problems programmers have with Derby and other databases I very often find application code like

Statement s = c.createStatement();
while (<some loop on j>) {
   ....
   ResultSet rs = s.executeQuery("select * from t where i > " + j);
   ....
}

This will result in a prepare (or compilation as some will call it) of the query each time executeQuery is called, and since the query will be different each time, no statement cache mechanism can help you avoid the compilation, either. Since prepare often is much heavier than execution, this will execute pretty slow.

Preformance problem solved!

The problem is easily solved with the following pattern (which most programmers should know):

PreparedStatement ps = c.prepareStatement("select * from t where i > ?");
while (<some loop on j>) {
   ps.setInt(1,j);
   ResultSet rs = ps.executeQuery();
   ...
}

Prepare is done only once, and if the loop iterates one zillion times, this really amounts to something!

Why?

There may be several possible explanations to this and one of them could be:

It's far too easy to concatenate strings in Java!

Programmers are obviously lazy (and need to meet deadlines), and the
last (and most efficient) alternative takes somewhat more energy to
write. I often find myself in situations where I use the first variant
because I'm in a hurry. I'm just writing a code piece for bug-finding,
some sample code or I thinking might optimize it later. I'm beginning
to wonder if it's a bad habit.

If you look at C code and ODBC, it's the other way
round. Programmers use the prepared statements and string
concatenation is a nuisance with strncat, strdup and strncpy, so in
ODBC, the last variant would be the easiest one to write.

Also done by tools

I have also found the same pattern in application code generated by some tools. I have even seen this:

while (<some loop on j>) {
   PreparedStatement ps = c.prepareStatement("select * from t where i > " + j);
   ResultSet rs = ps.executeQuery();
   ...
}

which for some databases will give you an extra round-trip to the database, too! (Derby may run in embedded mode where the extra procedure call does not matter much).

Be prepared!

The lesson from this is pretty simple:

Use prepared statements! (and don't forget to reuse them.)

Related Topics >>

JavaZone, Norway

Posted by bernt on September 17, 2005 at 7:09 AM PDT

Since this is my first blog on weblogs.java.net from a small country, Norway, I'll start with a short comment on Norway & Java.

Norway has a population of around 5 million people, no large cities, lots of beautiful nature, has become rich from vast oil resources and fisheries, and does not bother to be a part of the EU.

Small country? Definitely: Oslo, the capital, has around half a million inhabitants. In the third largest city, Trondheim, where I live, we are around 155000. But compared to many small countries in Europe, Norway is geographically large and sparsely populated (Trondheim is nearly 500 km north of Oslo).

Still, each year, the Norwegian Java User Group, JavaBin, hosts a large Java developer conference called JavaZone.

JavaZone gathers 1000 attendees and a host of speakers from several countries in 5 parallell tracks. I had the pleasure of speaking at JavaZone this year, and was impressed of both the quality and the diversity of the conference.

Not bad for a small country far north in Euorpe.