Category Archives: Database

How to improve your JPA Performance

James Sutherland has published a blog post showing how to significantly increase JPA performance.

In the post, he provides 11 different optimizations, such as pagination and query caching that can be applied to JPA.  The EclipseLink JPA 3.2 implementation of JPA is used throughout the post.

This is a good article and well worth reading by anyone that develops database systems using JPA.

Basic Java CRUD Operations with MongoDB

MongoDBIn this post I’d like to show how to perform basic CRUD operations against a MongoDB database using the Java driver.

For this post, lets assume that we have a todo database with a collection of todo items. Each item has a task and a priority.

In terms of JSON notation, an example item would look like:

{
  "_id" : { "$oid" : "4bffb75121eec88a67ff6ec8"} ,
  "task" : "Write Code" ,
  "priority" : "high"
}

Now that we have defined what we are storing in the database, lets have a look at how we connect to Mongo.

Connection to the database
To connect to a MongoDB database, we would use code similar to that below. In this code you can see that we are connecting to a database called todo and getting the collection called items. In MongoDB if neither of these items exist, they will be automatically created.

Mongo mongo = new Mongo();
DB db = mongo.getDB("todo");
DBCollection items = db.getCollection("items");

Creating Documents
To add a document to a collection, we use the insert() method of the collection.

BasicDBObject doc1 = new BasicDBObject();
doc1.put("task", "Write Code");
doc1.put("priority", "high");
items.insert(doc1);

Retrieving Documents
To retrieve documents from a collection, we can create a query and then iterate through it with a cursor.

BasicDBObject query = new BasicDBObject();
query.put("priority", "highest");
DBCursor cursor = items.find(query);
// Print out "highest" priority items
while (cursor.hasNext()) {
    System.out.println(cursor.next());
}

This query will find all the objects in the collection that have a priority of highest. If we wanted to get all of the items in the collection, we would create the cursor without a query as shown below.

DBCursor cursor = items.find();

Updating Documents
To update an object, we first have to get the object from the collection then we save it back into the collection.

BasicDBObject findTestItemQuery = new BasicDBObject();
findTestItemQuery.put("task", "Test Code");
DBCursor testItemsCursor = items.find(findTestItemQuery);
if(testItemsCursor.hasNext()) {
    DBObject testCodeItem = testItemsCursor.next();
    testCodeItem.put("task", "Test and Review Code");
    items.save(testCodeItem);
}

Deleting Documents
Finally, to delete a document or set of documents, we use the remove method of the collection.

BasicDBObject deleteQuery = new BasicDBObject();
deleteQuery.put("priority", "highest");
DBCursor cursor = items.find(deleteQuery);
while (cursor.hasNext()) {
    DBObject item = cursor.next();
    items.remove(item);
}

New Microsoft 2005 (2000) JDBC Driver Released

The new Microsoft SQL Server 2005 driver has been released and can be downloaded from here. The new driver is a type 4 driver that has been completely written from scratch and supports SQL Server 2000 as well as 2005. Accoring to MS, its been tested against Weblogic, Websphere, JBoss and Sun AS.

Currently I’m using the jTDS drivers for connection to SQL Server and they seem pretty reliable. Its probably a bit early for any comparisons (the new driver was only released yeaterday), but if anyone knows any, please post a link in the comments.

SQL Server – The Best Database in the World

OK, that’s maybe overstating it a bit, but I do think that the tools that come with SQL Server are very developer friendly. Take, for instance, the SQL Profiler. I find that this is particularly useful when writing any code that accesses databases, or for diagnosing database problems.

If you’re using Hibernate (and probably other libraries as well), you can get the sql dumped out to the console that is being executing. This is very useful most of the time, but it doesn’t contain the physical SQL that is going to be executed. It generally replaces the values from prepared statements with ‘?’s.

For debugging SQL related work its often invaluable to see the exact SQL that is being performed and on what connection – this can allow you to easily identify any potential database locking problems.

Running a SQL profile trace also allows you to see how long each query is taking and allows you to identify any potential bottlenecks in your application.

I know you can do similar things in Oracle (in version 9 at least – I’ve not seen 10 yet), but they’re just not as developer friendly. I’m not knocking Oracle, I think its a fantastic database also, however I feel it is let down by its developer toolset.