The other day, we had a very interesting debate about the role of the database in (our) Java/J2EE projects and architectures. Should you limit the use of the database to data storage and retrieval and maybe the odd index to improve query performance? Should you try to leverage every advanced database feature money already has bought? How important – or realistic – is it to make the application completely vendor independent? And is the only way of achieving the desired level of database independence completely abandoning all specific database functionality – only using the lowest common denominator between all database platforms you may ever want to migrate your application to?
The discussion was part of the design of the presentation – and its abstract – that my colleague Aino has entered for the JFall conference (October 12th in Ede, The Netherlands, see http://www.nljug.org/) on proper use of the Database in a Java/J2EE architecture: Database and Java: combined forces.
Our company has long tradition as an Oracle specialist – in 1999 we started using Java/J2EE in full swing. We know a lot about the Oracle database and all its capabilities. And while we must watch out for the ‘we have a hammer so that problem is definitely a nail’ syndrome, we have a distinct feeling that in many J2EE architectures and Java applications, the usage of the database and its specific powers is needlessly limited, often even to a damaging level when it comes to for example performance, security and scalability.
A very simple example of the typical dilemma we see quite frequently: we were developing an application with one of our customers. The customer wanted a clean, strict OO and J2EE-ish application. In this application, we had a requirement for copying a certain business object. This meant deep-cloning an object graph with up to several thousands of objects, most of which were not even instantiated in our middle tier. Running this copy operation in the Java Middle Tier, as OO theory and J2EE seemed to dictate, took close to 10 seconds and a lot of code. Implementing the same operation in the database resulted in about 0.3 seconds execution time and less than 15% of the original amount of code. In this situation, the database code was a piece of PL/SQL that primarily contained a number of SQL statements. The code was not directly portable to other databases, though it would be easy to implement the same Stored Procedure API – so that the JDBC call from the Java application would be the same on each database.
Another subject on which we often have raging debates is the implementation of (data oriented) business logic. Rules that dictate the state of the data. There are several considerations when discussing the place to implement these rules. There is the element of user feedback, which suggests client side implementation. There is the issue of data integrity above all, which suggests implementation in the database. Then there are the mechanisms, such as ApacheCommons (Struts) Validator, ADF BC Validations, Drools, Oracle Rules, Declarative Database Constraints etc. that each suggest a certain way of implementing the rules.
We believe in separating the tiers, defining clear responsibilities. However, we do not necessarily assume that the borders between the tiers also need to be the borders between the physical layers. Unless you define all columns in your database tables as VARCHAR2, there is always some validation of data going on when you insert of update the data. The discussion could be on how much validation – implicit because of conversion or explicit with constraints or triggers – should take place inside the database.
I do not accept an argument like: the database is (in) the data tier and therefore should not contain any business logic or validations. First of all, who says the database is only part of the data tier? Why should it not implement part of the business tier as well? Second of all, it is plain stupid – if at all possible- to ban all validations and business logic from the database. Not only is the database much better suited for simple things like uniqueness enforcement and referential integrity performance wise, it is also the only place where you can really guard the integrity of the data.
Part of the reasoning behind the split between Application Logic (Business Tier) and the Presentation Tier – the sound ambition to have multiple Applications or Presentation Components make use of the same Business Logic – forms a very important argument for implementing the Data Integrity rules or Data Oriented Business Rules inside the database. You Java application will very likely NOT be the only client of the database. It is extremely likely that at some point in time, other applications or processes or even the Database Administrator or Application Administrator, will also access the tables in your database. Through low level database operations such as Import/Export, through direct manipulation through SQL, through messaging applications or in other ways. But in my experience you are naive to trust that all database access will be through your Java based middle tier Business Logic Tier. So apart from scalability, security, performance and ease (== productivity) of programming, the crucial concern for data integrity should motivate you to implement the data rules in the database.
Modern databases – and that includes MySQL, PostgreSQL in addition to the commercial heavy lifting RDBMS’s – provide a wealth of functionality in addition to just storing and retrieving data. No one forces you to use even the smallest bit of this functionality. But not using it because you have some dogmatic point of view about simply not wanting to touch it is really inexcusable. And I have the impression that it is not at all uncommon to have people abstaining from doing anything but the most trivial things with their databases because of a sheer lack of knowledge. Trying to cover up for your lack of knowledge with partylines like ‘we must ensure complete database independence’ and ‘we need our application to be portable across databases’ is a much sadder approach than trying to find out what these modern database can offer and then based on the merits of each case deciding whether or not to use them.
Show me the organizations that switched databases – from Oracle 9i to DB2 for example. Show me the applications that needed to be migrated from one database platform to another. Now show me the organizations that spent thousands or hundreds of thousands of dollars or euros on their database – and then use it so sparingly that they might as well have ‘bought’ the mentally challenged brother of MySQL. And then see if that organizations perhaps spent way too much on development because everything was done the hard way – in the Java middle tier. And spent even more on fixing data issues because of integrity violations.
If database independence is an issue, and of course those situations do exist, think of better ways of achieving maximum or better yet: optimal independence without giving up on the real power of databases. Yes, even if your database is not an Oracle database.
Oracle 9i Database Features every Java developer can benefit from
I am not an expert when it comes to other databases besides Oracle. So I would not want to presume to make any suggestions on those databases. However, I am pretty sure that each database offers a lot of functionality that as a Java developer developing against such a database you really ought to be aware of.
For Oracle databases, there is a number of areas that I would like to mention in particular, primarily because these features have saved my day on numerous occasions. And knowing about them is really worth your time.
- Database Sequences (Oracle’s way of generating unique numbers), Before Row triggers and the Returning clause on Insert statements
- Database Triggers in general and Table and View triggers in particular – yes, you can define a trigger on a view; it is a so-called Instead-Of trigger. When you perform a DML statement (insert, update or delete) against a view with an Instead-Of trigger, rather than attempting to execute the DML on the underlying tables, the PL/SQL code in your trigger is executed. You have access to all old and new values on the record being manipulated. And you can do whatever you like in this trigger.
- PL/SQL in general, Table Functions in particular – where a PL/SQL function can be wrapped in a View as if it were a table. Imagine this statement:
select * from PLSQL_function. That is more or less what Table Functions allow you to do.
- Virtual Private Database (VPD), aka Row Level Security or Fine Grained Access Control. VPD has been developed especially for web-applications and ASP environments where multiple organisations have their data hosted in a single database. With VPD, each table is virtually partitioned: every user only sees a subset of records. However, there is only one table and the SQL and PL/SQL on the VPD-ed table is exactly the same as SQL against a non-VPD-ed table. VPD is particularly useful when depending on who has logged in to your web application, certain data should be visible. Note: for this feature, you require the Oracle Enterprise Edition.
- Declarative Integrity – the core value of relational databases is in integrity. Think Primary Keys, Unique Keys and Foreign Keys. No better performing, more solid mechanism for maintaining referential integrity and the uniqueness of logical keys. Oracle also offers check constraints for declaratively specifying and enforcing tuple rules – at the record level. Do not even think about implementing either Foreign Key or Unique Key in the Java Middle Tier.
- Ref Cursors – a great way of transfering data from a PL/SQL API to a Java application. Ref Cursors are easily defined in PL/SQL – using the SYS_REFCURSOR type – and can surprisingly easily be processed in Java: a Ref Cursor is converted into a JDBC ResultSet. Ref Cursors are ideal for hiding SQL inside the database while providing SQL-like access to data from the database
- Communication from the database – Oracle knows how to access Web Services, do HttpRequests (get or post) , send emails, communicate with message queues and write to and read from files
- XML functionality – the Oracle Database can easily return XML documents as query result; it can also validate XML against XSD, transform XML using XSLT, store XML documents either as relational data or as XML Document etc.
- Good Old SQL – You may think you know pretty much about SQL. I know I thought so, a year and a half ago. Then I discovered Analytical Functions, In Line Views, Hierarchical Queries (9i style), Merge operations, Materialized Views, Database Event triggers, Regular Expressions inside SQL and many more useful syntax, functions and functionality (this resulted in our Oracle 7Up Workshop). Take a look. You might just be surprised.
The first step every Java/J2EE architect should take is be up to speed with the capabilities of the database you have at your disposal. Going from there – with an open mind towards what ought-to-be and what-should in terms of ‘proper’ implementation of functionality and even with regard to traditional boundaries between logical Tiers – decide how to implement functional requirements. Be pragmatical about them, not theoretical or even worse: dogmatic. There are many things that have been optimized in the database kernel. Things your Java code will never be able to do better – in a realistic timeframe. Aggregations, uniquess enforcement, data auditing, security, scalability, copy of large volumes of data, bulk updating or deleting are just a few examples of things that the database is simply better at.
If database portability really is a concern, try to access the database through simple APIs, SQL and Stored Procedure based interfaces, that can be defined in any serious database. The implementation can be different between databases, but your application only converses with the interfaces.
Consider whether the database is really only good for data tier stuff. Dumb data stuff. Be sure that you know what can be done before deciding you do not want it. Do not develop on an $100000 Enterprise Edition database as if it were Microsoft Access. Unless you want keep open the option to migrate to MS Access at some point in the future.
5 thoughts on “Role of the Database in a J2EE Architecture – Dumb Data Store? Only Data Tier? Also Business Tier?”
Interesting comments. I find that you are a bit frustrated that some Java developers are
not familiar to the database funtionality. The decisions on what features to use
should have been made by the application architect who should have broad awareness
of the selected product’s capabilities.
One of the ideas of J2EE is to seperate the business logic and the data tier
depending on the requirements & complexity of the application. It should stop you
from leveraging the database features to give better performance or productivity.
I think 100% database independance is not feasible but by using good
development practice (eg. DAO Pattern), work can be isolated and reduced
when porting from one DB to another.
I could not agree more here, especially in the area of data integrity rules.
They should be enforced at the RDBMS-level, period (no discussion possible).
Regarding J2EE, there is one aspect that you do not touch: its (immense) complexity.
Don’t get me wrong, I love Java, it is a beautiful programming language. But I still get
amazed by how complex this whole J2EE monster on top of Java, has been made.
The amount of knowledge a simple developer must have to be productive in this area, is
orders of magnitudes more than used to be. And it is even worse when maintaining (ie. not
building new) existing software.
How does this benefit ‘the customer’, who just wants a part of the business supported by some
IT application system…
Honestly, your arguments above have already (a long time ago) convinced me that the whole J2EE approach is wrong.
Oracle should take another look at PL/SQL and DB objects and “J2EEify” it. Why shouldn’t I be able to build an application using database objects the same way I can build an object using J2EE?
Unfortunately, Oracle seems to be drinking the Java Koolade… have you noticed that many of their newer products (ie. Collaboration Suite and iFS no longer have PL/SQL APIs?
Well stated Lucas. You’re very right in that the key isn’t “database independence”
(no reliance on the database doing anything). Rather it is “optimal independence without
giving up on the real power of databases”.
One past experience stands out. In a mixed skills shop, we actually had the old Forms folks,
the DBAs, and the new Java folks sit down with a taxonomy of business rule types (yes, from
Rule Frame). And decided, as a project standard, what types of rules should be implemented where.
And how. Everyone learned. And learned to appreciate the capabilities of other layers (like not
having to deal with mutating data in triggers — do it in the middle tier!!).
It’s all about the best use that gets the best result. Not about the dogma!!
Great post! I knew most of the db features that you mentioned, but the “communication from the database” section really caught my attention. You’re right though in that if you’ve spent several thousand dollars on a db, you better use it. I think the problem with a lot of development companies is that they didn’t invest in good DBA’s who know of these features. I’ve been fortunate in that the company I work for has three awesome DBA’s who really go out of their way to educate us Java developers. They’ve also saved our butts (mine included) on several occasions. A good DBA is worth his/her weight in gold!
Comments are closed.