Book Image

Tapestry 5: Building Web Applications

Book Image

Tapestry 5: Building Web Applications

Overview of this book

Table of Contents (17 chapters)
Tapestry 5
Credits
About the Author
About the Reviewers
Preface
Foreword
Where to Go Next

Appendix B. Creating a Real Data Source with db4o

Throughout this book we have used a mock data source—a simple Java class which only imitated storing and retrieving data. This wasn't very exciting, but dealing with a real database normally means a lot of distractions. We would have to download such a database, install and configure it, and create the necessary tables in one or another way.

In order to work in a contemporary way, we might also wish to use some object-relational mapping solution like Hibernate. That would mean downloading, installing, configuring, and then writing a lot of XML, all from scratch to achieve what we want.

I would not dare to explain how to do all this in an appendix. Fortunately, there is a fantastic alternative—to use an object database named db4o. All you need to do to get this database is to download the most recent version from the project's website (http://www.db4o.com), extract an appropriate JAR file from the package and drop it into the WEB-INF/lib subdirectory of your Java web application. That's it; your new database is ready!

But what is most amazing is that db4o is not just a toy to play with in an example project. It can be easily used in a real life web application. Here is what the creators of db4o write about it:

Note

...it allows you to store even the most complex object structures with ease, while achieving highest level of performance. Database benchmarks show db4o to be up to 55 times faster than Hibernate and MySQL, a popular object-relational mapper and relational SQL database stack.

For further information please refer to http://www.db4o.com/about/productinformation/.

As an additional bonus for us, db4o comes with an excellent interactive tutorial that you will find inside the downloaded package. I wholeheartedly recommend that you read this tutorial—you will understand how to deal with this unbelievable database in no time.

Here, you will find a demonstration of how db4o can be used to create a real data source for our Celebrity Collector application.

Preparing the Database

The package you download from the previously mentioned website will be named something like db40-6.3-java.zip, although the version number might be different. In its lib subfolder, you will find a number of JAR files—for different versions of Java. We need the one that works with Java 5, it will be named like db4o-6.3-java5.jar. Copy this file to the WEB-INF/lib subdirectory of the Celebrity Collector project.

The last piece of "configuration" is to decide where you are going to store the database file on your hard drive. You can name it whatever you wish and you will be able to copy, distribute, or back it up like any other file. Let's say our file will be at C:\tapestry5\data\celebrities.dat.