Slashdot Mirror


Java Database Best Practices

ProfKyne writes "I was excited to read this book when I first noticed it listed as "coming soon" in the Java section of O'Reilly's site back in May. I downloaded and read the sample chapter and waited expectantly for the book to be released. I was hoping that this book would be perfect for me, as nearly all of the Java programming I have done has dealt in some way with database access, though I do not consider myself to be an expert on the subject. The book didn't disappoint; Java Database Best Practices is an excellent high-level guide to writing database-driven applications from Java." Read on for the rest of ProfKyne's review. Java Database Best Practices author George Reese pages 267 publisher O'Reilly Associates rating Excellent for Java programmers moving to J2EE reviewer Erik Price ISBN 0596005229 summary A high-level introduction to writing database-driven apps in Java

What it's about: The book is divided into three sections of several chapters each:
  1. Data Architecture - an introduction to database-driven application concepts
  2. Persistence Fundamentals - descriptions of the various persistence frameworks for Java application development
  3. Tutorials
I should note that the book follows a strange order: the most logical progression for the apparent target audience of this book (developers just getting into J2EE) would be to read Part 1 first, then Part 3, and then Part 2, as the "Persistence Fundamentals" section draws heavily on content introduced in the later "Tutorials" section.

The first section (Data Architecture) introduces some of the concepts that will be covered in greater depth later on in the book, but also contains an excellent chapter, "Relational Data Architecture," on database schema design and normalization. By the way, this is the sample chapter that can be downloaded from O'Reilly's site. In fact, this chapter set itself apart from the rest of the book in that it goes into a great deal of detail on its subject (although obviously an aspiring DBA should probably read whole books dedicated to the topic of relational database concepts). Most of the rest of the chapters in the book is somewhat more lightweight than "Relational Data Architecture." This section also contains a chapter that introduces some of the things to be aware of when managing transactions, including implementation tips on using transactions in JDBC programming such as optimistic locking and the various transaction types (dirty reads, etc).

The Persistence Models section focuses on different persistence frameworks, there is a chapter for each of the following: Persistence Fundamentals, EJB Container-Managed Persistence (covers 1.x and 2.0 models), EJB Bean-Managed Persistence, JDO, and "Alternative Persistence." The fundamentals chapter is where some of the best of the "best practices" of the book are found, and introduces such patterns as the Data Access Object pattern and the Memento pattern in the context of a simple guest book JSP application. The EJB chapters do assume that the reader has a basic understanding of how EJB works, and provides suggestions and guidance on ways to implement EJBs in real J2EE applications. (Don't worry, if you don't know about EJBs, the J2EE Tutorial chapter later in the book provides this.) The JDO section is one of the shortest in the book; I think that it's just such a new technology (in terms of available implementations) that most of the "best practices" are yet to be discovered. "Alternative Persistence" means Hibernate and Castor.

The Tutorials section is definitely not for advanced Java programmers, as it contains beginner-level introductions to the J2EE platform, the SQL language, the JDBC architecture, and the JDO API. If you really don't know anything about JNDI or servlets or EJB, then the J2EE chapter is written for you -- it's not a "how-to" tutorial, but rather a description of the basic concepts. The JDO tutorial is like the J2EE tutorial, introducing the basic concepts such as "the extent" and the filter language used to query for JDO-persisted objects, but I think partly because it's such a new technology, there's not a lot of detail here. You'll still have to read more about J2EE EJB, or JDO, if you decide to use them in a production application. On the other hand, you will learn how to make effective use of SQL queries and the JDBC API (which lets you execute SQL queries from your Java code) from their respective tutorials.

While this is a good start, you will eventually want something more in-depth than this if you are actually implementing a J2EE application (such as a book dedicated to servlet/JSP programming or Enterprise JavaBean development). But I really wish that I had read Java Database Best Practices before tackling these subjects, as it would have helped ease the transition

What's to like As strange as it may sound, one of the things I liked most about the book is that it is relatively thin. In other words, the author gets right to the point and doesn't waste excessive space replicating information that can be found elsewhere. For instance, when describing how to implement a database-independent sequencing component, he incorporates enough code to support what he's saying, but doesn't include page after page of complete code listing, intact with multi-line Javadocs and plenty of whitespace -- just the meat. You can download all of the code from O'Reilly's site in a zip archive (and yes, it does include the Javadocs and whitespace that are wisely omitted from the book). Ironically, the book costs no less than other offerings from O'Reilly that are twice as thick.

Also, the "best practices" in the book are very good and, at least in my case, I was not familiar with some of them, so it was very worthwhile reading. As an example, I myself have recently run into the problem of different sequencing systems on different databases (such as MySQL's AUTO_INCREMENT column type vs PostgreSQL's sequence types). I've worked around it by modifying the application's database calls, but that isn't really a good strategy. Implementing a sequencer at the application level (one of the "best practices") is a much better idea if you're not clustering your application servers and other applications will not be performing INSERTs into the database. On the other hand, if clustering is an issue, then it's probably a better idea to use EJBs, and if other applications will be writing to the data store, then database portability will be difficult in the first place (even with the help of Perl's DBI or PHP's PEAR DB abstraction). Some of the other excellent best practices introduced are the DAO pattern and database schema normalization coverage.

What's to consider The most important thing to know when considering this book is that it really struck me as being appropriate for novice J2EE developers and/or those who are new to database programming in general. First of all, I think it's safe to assume that most people who will be doing anything with databases in Java will be using at least some element of the J2EE platform, and this book makes that same assumption. Java's tremendous success right now is in server-side application development. This means there won't be as much useful information in this book for the folks who are writing, say, an AWT interface tightly coupled to a MS-Access database through ODBC calls. Second of all, the patterns and practices described in this book should be familiar to most people who are already architecting J2EE applications, and are covered in greater depth in other books on enterprise application development.

Having now covered the main consideration, I do have to pick a couple of nits. Firstly, this book does suffer from a few of the editing problems that plague nearly all computer books (yes even the O'Reilly books) in their first printing. It's kind of embarrassing, but even in the sample chapter I found a somewhat bizarre mistake near the end, where a class diagram is drawn up using ERD notation instead of UML. Fortunately, there aren't too many technical errors like this. Another bone to pick was the author's choice to use a hideous scriptlet-heavy JSP instead of a servlet forwarding to a JSP in the small Guest Book application in "Persistence Fundamentals." I know that this is Java Database Best Practices, not Java Model View Controller Best Practices, but scriptlets are both difficult to read and quite frankly scorned as J2EE moves toward JSTL and JSP 2.0. It's a little too bad that View and Controller are mixed up in this example, because the author does an excellent job of explaining how to implement the Model using the Data Access Object pattern.

Finally, I have a feeling this book was named "Java Database Best Practices" for marketing reasons, perhaps to go with O'Reilly's other offering, "Java Enterprise Best Practices". While this is an excellent book about Java database programming, and there are indeed dozens of "Best Practice" tips strewn throughout the book, I think a better name for this book would have been "Java Database Fundamentals."

The summary I didn't want to simplify and rehash the book's "best practices" contents in this review, but rather provide prospective readers with an idea of what to expect from this book. Personally, I got a lot out of the book, but if you've already implemented bean-managed persistence in your EJBs using JDO, then you've probably already gotten past the point where this book is going to be a big help. In your case, it might be worth having around the office if you can get the company to pay for it. However, for those who have learned the Java language and want to get more involved in server-side application development, you're going to want to make sure you're familiar with all of these concepts and this book is the perfect way to get started with that. Where to find more info I'm not going to replicate the table of contents or any of the content in the book, because all of that information as well as the excellent sample chapter are available from O'Reilly's site. I purchased the book from SoftPro Books in Massachusetts (US), a bookstore for computer geeks. This is a good place to peruse the book if you're still riding the fence.

You can purchase the Java Database Best Practices from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

9 of 180 comments (clear)

  1. Sounds like a winner... by Gortbusters.org · · Score: 3, Interesting

    Seriously, whenever the question of C++ vs Java comes up in a database environment the C++ in ya just wonders what can be done in Java. This book looks like it will shed some light on what ya should do and the various avenues to explore with databases and java.

    Let's all hop on the J-Band Wagon!

    --
    --------
    Free your mind.
    1. Re:Sounds like a winner... by Anonymous Coward · · Score: 1, Interesting

      I don't see what bound parameters get you that jdbc PreparedStatements don't. Oh, except I have to do a virtual lookup which takes maybe a couple dozen cpu cycles. If you're trying to get performance gains of that level then your database design and middleware design and deployment must be god-like in its perfection.

      I agree with what others said: design is all here, a good dbase design and a clean middleware design to do sensible dbase hits (most importantly, to not hit the dbase more often than you need to, and to minimise holding dbase locks that block other transactions), is the biggest performance thing around.

      Dbase design is especially important for real enterprise apps: keep the dbase layer design clean and then you can scale your app servers and web servers horizontally to get as much throughput as any app could ever need.

      We're replacing a two-tier system that's heavily stored-proc based with a java app, and improving the performance of the apps key processes as we move it from ugly nested cursors to clean java code. But to achieve that you've got to be J2EE expert: that is, an expert on all 3 tiers, not just someone who does Java.

      Sean

  2. Sounds like it is missing... by under_score · · Score: 3, Interesting

    good information about one of the most important aspects of persistance in an object oriented language: Object Relational Mapping. I haven't read the book myself, but if it doesn't cover Java OR mapping layers such as Cocobase, Toplink and EOF (part of WebObjects), or any of the open source OR mapping frameworks, and the theoretical foundation they are all based on, then to me it isn't worth it.

  3. Ok here is a java question by ACK!! · · Score: 2, Interesting

    How many people out there use java in their web based apps that interface with a database server backend?

    What platforms do you use for development?

    What Dbs do you use?

    What web server?

    Why?

    Inquiring minds want to check your java intake and output.

    --
    ACK /ak/ interj. 2. [from the comic strip "Bloom County"] An exclamation of surprised disgust, esp. i
    1. Re:Ok here is a java question by pmz · · Score: 3, Interesting

      How many people out there use java in their web based apps that interface with a database server backend?

      I would bet that nearly all the use of Java is for web-based apps that interface to a database.

      What platforms do you use for development?

      For Java, it would be J2SE from Sun (it includes JDBC).

      What Dbs do you use?

      Oracle, MySQL, PostgreSQL, DB2, etc.

      What web server?

      Apache, WebLogic, Sun ONE, etc.

      These questions, at face value, mainly answer themselves. Perhaps a better question would be: what do Java programmers do to prevent database applications from turning into a quagmire of naive mistakes and bottlenecks making a well-intentioned application into a sick joke of engineering?

      Database programming is one of those things that is simple enough, at first glance, to be attempted by every college sophomore on the planet, but complex enough, in reality, to make people who hired those college sophomores look foolish.

    2. Re:Ok here is a java question by msobkow · · Score: 4, Interesting

      Platform doesn't matter much. Linux, Win32, AIX, Solaris -- they've all got decent Java IDEs and JDKs.

      Databases just need to be full-featured. Sybase, DB/2, Oracle, PostgreSQL -- I don't care as long as it's got a full feature set including sequencers, triggers, and stored procs (in that order of usefulness.)

      Web servers are irrelevant to the back end development. The web server has hooks that redirect certain pages or page types to the "rich" environment of a J2EE or equivalent framework. Even Apache doesn't embed Tomcat, but cooperates with it to share the duties (let Apache do what it does best -- server up simple content, and let Tomcat/J2EE/JSP do what it does best -- serve up functionality with a corresponding performance hit.)

      --
      I do not fail; I succeed at finding out what does not work.
    3. Re:Ok here is a java question by markv242 · · Score: 2, Interesting
      What platforms do you use for development?
      NetBeans hooked into a CVS server. It offers a huge amount of functionality (nearly competitive with JBuilder) and it is free.
      What Dbs do you use?
      Oracle 8i, although we're migrating to Oracle 9 fairly shortly. High-availability, clustering, and reliability. Oracle cannot be beaten.
      What web server?
      Apache 2.0 in "worker" MPM mode. Multi-threaded, multi-process. Provides the stability of Apache 1.3 (multi-process so if one process crashes, no problem, they just get started right back up) along with a huge speed increase due to the multithreading. Changing from Apache 1.3 to Apache 2.0 in worker mode, tuned correctly, will give you a 500% capacity and speed boost, easily. We have gone from approximately 85 simultaneous connections max, running at about 200 requests per second, to over 2500 simultaneous connections running at 900 requests per second, on the same hardware.
      Why?
      You forgot the most important question. "What application server are you using?" We aren't using EJBs, so we only need to run a servlet runner. Resin is an amazingly quick app server for our purposes.
  4. Re:"Best Practices" by smack.addict · · Score: 4, Interesting
    Disclaimer: I am the author of the book being reviewed.

    I have never quite figured out why some developers have a visceral negative reaction to the expression "best practices". In any field, a best practice is nothing more than a rule of thumb that guides your decision making. It is a heuristic.

    Best practices cannot be fully incorporated into a language. Java does a good job of that, but new best practices grow from further use. As the reviewer noted, the chapters on newer technologies (e.g. JDO) were lighter in this book because programmers simply have not had as much experience in developing best practices with those technologies as with older ones (e.g. JDBC).

    Furthermore, a best practice is not something you get sent to the electric chair for violating. When your decision violates a best practice, the "best practice" status simply indicates that you need to understand why it is a best practice and make sure you go to great lengths to justify its violation. On the other hand, you do not need to think a lot about sticking with a best practice.

    By leveraging best practices, you simply gain a short-cut in decision-making. For those people who would say "short-cuts in decision making are bad", get real. Short cuts are necessary. You cannot analyze all possibilities resulting from every decision you make. Otherwise, you would never manage to flip a switch to turn on a light bulb.

  5. Re:hmmm by Anonymous Coward · · Score: 2, Interesting
    There is alot of confusion around proper code practices. A better discussion is taking place on theserverside.com. Some people are too entrenched to change away from the rdbms centric approach(stored procedures, triggers, etc.). But for the rest of us, I recommend looking into the JCS code which when matched with an OR tool, gives the programmer ideal mapping tools. Apache is working slowly on a fine tool called OJB that will hopefully someday include full JDO support. Let them know you're interested. CMP - Container Managed Persistence BMP - Bean Managed Persistence JDO - Java Database Objects EJB - Enterprise Java Beans Here's a summary of the future for those of you who just want to observe from the gallery. I prefer open source tools personally, so most of the links are for those tools:
    EJB is problematic. Most(80% I'm guestimating) of programmers don't understand it but think they do. It is not 'THE' CMP solution, but one of them. Containers give you the ability to do clustering between app servers, which is a good thing. But EJB isn't the only one, compared set against ATG's superior repository API. Scope in EJB is also confusing and most programmers get this wrong, but the mapping is thus: request scope: stateless session bean session scope: statefull session bean global scope: entity bean If you don't understand scope, that is a different issue. But it is necessary for proper app server design to know the difference and use them correctly. JDO is most likely the future. It can or may not use a container. And gets around the flaws of every other mapping tool. Here is a synopsys of their flaws: A better architecture does not alter itself to fit the tools one is using but best fits the problem to be solved. For example, most tools like hibernate and castor suffer from the need to force the bean that is persisted to use public getters and setters. If you want to make your getter and/or setter private, default or protected scope, then you're out of luck. JDO doesn't have this limitation. In many designs, not every column in a table needs to be exposed to calling classes. EJB requires that a persisted bean implement an interface or subclass a parent. This also alters the design, and JDO makes no such requirements. Staying true to the best design to fit the problem, will give you the best return. Naturally, the world is moving away from stored procedures, joins and the like. Yes, really it is, you don't have to like it, just live with it. Although most of the foot dragging is by those who's past history doesn't include multi-threading and good OO design. Perl is a prime contributer here. Although Perl can do in memory object caching, most Perl mongers aren't familiar with it. It's much easier in Java though. But in-memory caching is a must have. That is the key concept behind best java database accessing practices. Using only request scoepd objects is less efficient.
    So by next year, JDO will be production ready. Until then use EJB or Cocobase, Castor, Hibernate or the like($) if you must have cluster(multiple app server) support. Otherwise, good old SQL(use a config file to load the SQL statements to avoid embedding) with a BMP design will do quite fine for a single box. Also, design from the object level, and not from the table level. This is heresey to some, but it is a (better)best practice.
    Note:
    With the release of JDK1.5, the use of external XML configuration files will be reduced or eliminated since source code markup will be added.