O'Reilly logo

Java Database Best Practices by George Reese

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required


It is never too late to become reasonable and wise; but if the insight comes late, there is always more difficulty in starting the change.

Immanuel Kant, Prolegomena to Any Future Metaphysics

Java database programming has grown much more complex than it was in 1996 when I wrote the first edition of my book Database Programming with JDBC and Java (O’Reilly & Associates). The J2EE platform did not exist. Distributed programming was RMI, JDBC was simple, and transaction management and persistence did not exist in the Java vocabulary. Database programming in 1996 was quite simply JDBC programming.

To place database programming in a real-world context, I spent much of that book introducing ways to build robust persistence models and manage transactions using only the JDBC API. As you can imagine, you had to do a lot of things for yourself that developers now take for granted in the Java platform.

The Java world has certainly changed since then. Not only does Java provide you with a persistence model, it provides you with three different persistence models built right into the core J2EE platform. Outside the J2EE platform is the popular JDO persistence model. In addition, many tools exist to enable you to effectively use third-party and custom persistence models. All of these choices present a problem for database programmers that simply did not exist in 1996: what are the best approaches to database programming with the Java language?

This book seeks to aid the Java developer in appreciating the different approaches Java provides for database programming. It helps you assess what approaches fit which problems, and what the best practices are under each model.


This book is not an introductory text. It is also not a tutorial on any particular API. It is, instead, a description of the best practices for using a database to drive a variety of Java application architectures. It assumes you have at least a passing familiarity with one or more of the Java enterprise APIs, as well as SQL. You do not, however, need to be an expert in all of them. To help you with any holes in your knowledge of these tools, I provide a few tutorial chapters at the end of the book.

Organization of This Book

This book is divided into three distinct sections. The first two sections are the meat of this book: best practices for Java database architecture and development. The first section focuses on the architecture aspect and the second section on the development aspect.

Part I

Chapter 1 is an overview of the art of database programming. It examines the various tools and skills needed for database programming and covers common database application architectures. The chapter is mostly review material for experienced database programmers.

Chapter 2 tackles one of the more difficult aspects of database programming, especially for the object-oriented programmer: data architecture. This chapter begins with relational theory and covers critical topics such as normalization and object-relational modeling. It is a very important chapter for database programmers of all levels of experience.

Though relational architecture is one of the more difficult aspects of database programming, transaction management is where database programmers make most of their mistakes. Chapter 3 covers transactions and transaction management.

Part II

The second section begins with an overview of persistence concepts. In short, persistence is the practice of saving application state to a data store. Chapter 4 introduces this practice with an eye on using relational databases as your data store for Java applications.

Chapter 5 through Chapter 8 go into the best practices for different Java persistence models. Chapter 5 begins with container-managed persistence under the Enterprise JavaBeans component model—for Versions 1 and 2. Chapter 6 tackles the other EJB persistence model, bean-managed persistence. Chapter 7 dives into an evolving, popular persistence model, Java Data Objects. Finally, Chapter 8 looks at alternatives to the standard Java persistence models.

Part III

The third section of the book contains tutorials on the core technologies covered in this book. No reader should need to read all of the tutorial chapters. Instead, I expect that most readers will be familiar with the subject in several, but not all, of the tutorial chapters. The tutorial chapters provide the basic knowledge necessary to understand key concepts used in the first two sections. Don’t look to any of the tutorial chapters to make you an expert in its subject matter. I have provided tutorials on the J2EE platform (Chapter 9), SQL (Chapter 10), JDBC (Chapter 11), and JDO (Chapter 12).

I recommend reading the first two sections in order, breaking that order only to refer to a tutorial chapter for a subject on which you lack familiarity.

Conventions Used in This Book

The following typographical conventions are used in this book:


Used for filenames and directory names, programs, compilers, tools, utilities, URLs, emphasis, and first use of a technical term.

Constant width

Used in code examples and to show the contents of files. Also used for tags, attributes, and environment variable names appearing in the text.

Constant width italic

Used as a placeholder to indicate an item that should be replaced with an actual value in your program.

Constant width bold

Used to highlight a particular section or change in code, such as a custom tag or a change in a transaction.

Comments and Questions

Please address comments and questions concerning this book to the publisher:

O’Reilly & Associates, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
(800) 998-9938 (in the United States or Canada)
(707) 829-0515 (international/local)
(707) 829-0104 (fax)

There is a web page for this book, which lists errata, examples, or any additional information. You can access this page at:


To comment or ask technical questions about this book, send email to:

For more information about books, conferences, Resource Centers, and the O’Reilly Network, see the O’Reilly web site at:


About the Philosophers

Daniel Dennett (Chapter 1)

Dennett, who teaches at Tufts University, is probably my favorite philosopher. His books are actually well written, which is a rare quality among philosophy texts. His works run the spectrum of philosophy, but his greatest influence lies in the philosophies of mind and science. If you want a fun philosophy book to read that does not require you to be a philosopher, pick up his book Elbow Room. If you are looking for something more weighty, but equally accessible, read Darwin’s Dangerous Idea.

René Descartes (Chapter 2)

Though he lived from 1596 until 1650, Descartes’s writings mark the beginning of modern philosophy. He was a French philosopher who emphasized a solipsistic approach to epistemology. He is the author of the famous quote “Cogito, ergo sum,” or “I think, therefore I am.”

Donald Davidson (Chapter 3)

Donald Davidson is among the most important philosophers of the late 20th century. He is particularly influential in the philosophy of language and action theory. He is currently a professor at the University of California, Berkeley. My senior thesis at Bates College was based on his writings.

Ludwig Wittgenstein (Chapter 4)

Ludwig Wittgenstein was a German philosopher who lived from 1889 until 1951. His primary contributions to philosophy were in the philosophy of language. He once wrote that “philosophy is a battle against the bewitchment of our intelligence by means of language.”

Friedrich Nietzsche (Chapter 5)

Nietzsche, who lived in Germany from 1844 until 1900, is likely the most controversial “serious” philosopher. His writings have influenced nearly every kind of philosophy but have had their greatest impact—both positive and negative—in the area of ethics.

Martin Heidegger (Chapter 6)

Heidegger, another 20th-century German philosopher, made popular the movement started by Edmund Husserl known as phenomenology. Phenomenology attempts to understand things as they present themselves rather than to appeal to some sort of essential nature hidden from us. This movement eventually led to the most popularly known philosophical movement, existentialism.

David Kolb (Chapter 7)

David Kolb was my major adviser at Bates College in Lewiston, Maine, where he is a Charles A. Dana Professor of Philosophy. He has written extensively on Hegelian philosophy and nonlinear writing in philosophy.

Immanuel Kant (Preface, Chapter 8)

Immanuel Kant may be the most influential philosopher of the second millennium. He was a German philosopher who lived from 1724 until 1804. He emphasized a rational approach to all philosophical pursuits. This rationalism has had its greatest impact in the area of ethics, where moral principles are, according to Kant, derived entirely from reason.

David Hume (Chapter 9)

David Hume was an 18th-century Scottish philosopher who wrote on a range of philosophical subjects. He is largely responsible for the school of philosophy known as empiricism.

Ruth Garrett Millikan (Chapter 10)

Ruth Garrett Millikan is a professor of philosophy at the University of Connecticut. She is an influential modern philosopher in the philosophy of language and epistemology.

Noam Chomsky (Chapter 11)

Born in 1928, Noam Chomsky is perhaps the most famous living philosopher. While often known for his political activism—especially during the Vietnam era—his greatest contributions to philosophy lie in the philosophy of language.

Jean-Paul Sartre (Chapter 12)

Sartre was a novelist, a philosopher, and a member of the French Resistance during World War II. As a philosopher, he is best known as the force behind the existentialism movement. Existentialism goes beyond phenomenology in its claims about the essential nature of things. While phenomenology claims that we should not appeal to an essential nature of a thing in order to understand it, existentialism says that no such essential nature exists. A thing is exactly as it presents itself.


So much work other than that of the author goes into putting together a solid book. First of all, Brett McLaughlin’s editing skills and general Java knowledge have been critical to keeping me in line. Also critical to the book was the contribution of Chapter 8 on alternative persistence frameworks by Justen Stepka. I am not much of a fan of leaving the core platform, so this book would have been incomplete without his contribution.

Several people contributed to reviewing this book: Nick Kokotovich, Justen Stepka, and Henri Yandell. In addition, Monique Girgis, Andy Oram, and John Viega have all at times provided a critical eye on this content.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required