People are still writing straight JDBC Code?
I’ve been using Hibernate, JPA, and the Spring Framework for so long now, that I tend to forget that there are still apparently lots of Java developers out there still entirely hand-writing JDBC code – getConnection(), etc., and trying (sometimes) to manage the JDBC resources properly in the face of exceptions.
Two things bring this to mind.
First, I recently did a consulting gig for a client who had hand-written JDBC transactions, and they were experiencing locking issues caused by transactions that were leaked when an exception was thrown.
Second was this blog post where the author suggests a few ways to write “pretty database code” that automagically cleans up resources that would otherwise be leaked.
Unfortunately, the author is trying to patch a problem that he shouldn’t have created in the first place. Instead of annotating resources to be cleaned up, I would recommend the use of a library such Spring (see JDBCTemplate) that abstracts the resource management, so that you don’t have to deal with it at all.
In the case of the client mentioned earlier (who was using Spring and JPA), user-transactions should not have been used, when JPA, or even Spring’s JDBCTemplate could have been used to manage the transactions.
The moral of the story is, if you are doing something in your code repeatedly that is complex or requires tricky resource management, others have probably had the same problem, and someone has probably solved the problem elegantly. Look for the elegant solution. Google is your friend. Don’t cut-and-paste, and for heaven’s sake, don’t create a macro in your IDE to do the cut-and-paste for you.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)






Comments
cowwoc replied on Thu, 2011/02/10 - 2:55pm
Take it from someone who's used Hibernate for over 5 years, ORMs are the crap. Ted Neward had it right: http://blogs.tedneward.com/2006/06/26/The+Vietnam+Of+Computer+Science.aspx
I personally recommend the following combination:
1. Plain JDBC
2. querydsl-sql for type-safe queries: http://www.querydsl.com/
3. MyBatis Migrations or Flyway Migrations for database versioning
ORMs might seem easier/faster at the beginning but you'll end up with a higher learning curve and more edge cases than the above combination. Your overall productivity will actually be higher using straight JDBC and you'll end up with simple code.
Andy Leung replied on Thu, 2011/02/10 - 2:59pm
Jilles Van Gurp replied on Thu, 2011/02/10 - 3:09pm
I actually switched back from JPA/hibernate to raw JDBC after observing the following things:
1) People in my team were blindly trusting Hibernate to do the right things. It wasn't and it proved very hard to fix & debug on multiple occasions. We had a long succession of very obscure bugs that essentially boiled down to hibernate doing things we did not intend or not doing things we actually wanted it to do.
2) The amount of magic was such that people were assuming things to happen that actually weren't happening at all or at the wrong moment. Things like transactions for example. Just because it has an annotation, doesn't mean you've done your job. When the magic stops working you have a problem.
3) It was painfully slow without any obvious reason. Part of the reason was that an optimized database schema looks quite a bit different from what hibernate produces from what looks like an innocent little data model.
4) People were thinking they were doing OO development instead of database development. That's an illusion that hibernate is designed to provide but if you fall for it it will prove to be fatal. We fell for it and our data model turned into a ridiculously normalized mess with way too many tables. Hibernate + junior engineers is a disastrous combination.
So we did the following:
1) we re-developed the worst offending code from scratch
2) we used plain old jdbc only (very refreshing experience, it's not complicated at all).
3) we kept the data model & database schema for compatibility reasons
4) we offloaded most our complex querying to solr. This gave us a massive boost in performance & scalability as well as ease of use.
This fixed our performance and complexity issues in a pretty dramatic way. Also , the resulting code is quite small & simple. I haven't had to touch it in months since it just works.
We could have probably achieved the same results with hibernate and a complete redesign. But I don't believe the added complexity is worth the magic. I'm a strong believer of keeping things small and simple.
Wujek Srujek replied on Thu, 2011/02/10 - 3:53pm
That's what you usually get for using tools that you don't understand / know well enough to use them correctly. As a junionr I saw usages of Hibernate implemented by seniors in such a way that I wanted to cry - there was no understanding of anything, and it was because nobody ever read anything on the topic. Was this ego? Lack of time? I have no idea; what I know is that the code was utter nonsense, which was not fault of the tool, but of the people who used it incorrectly.
As for the auto-generated schema - nobody on the Hibernate team ever said you should use it for production. It is only natural to employ a DBA for 2 straight days / weeks to pimp it up.
That being said, I prefer other tools to Hibernate.
Fab Mars replied on Thu, 2011/02/10 - 6:34pm
I still find JPA/Hibernate very convenient for CRUD-like screens. And I agree with the previous commenter: 80-90% of the people I worked with just didn't understand what they were doing with Hibernate.
From my experience it's better to use:
- ORMs for CRUDs (eg administration sections of your app)
- SQL/StoredProcs for front end stuff (eg parts with lots of hits or searches)
Jarrod Roberson replied on Thu, 2011/02/10 - 6:42pm
Steven Yong replied on Thu, 2011/02/10 - 9:14pm
Fabien Bergeret replied on Fri, 2011/02/11 - 1:27am
Hibernate or JPA can be great productivity tools, but they have to be used very cautiously, under the supervision of experienced people. As a technical team leader, I'd certainly recommand it neither for a junior team, nor for a big expericeed team.
Liam Knox replied on Fri, 2011/02/11 - 3:51am
1. There seems no reason to favour straight JDBC when you have Spring's excellent JDBC support which hides the resource management and general gunk and comes also with great transactional support. I would always use Spring over straight JDBC
2. ORM's have their place but they are not the solution for all problems. I wouldn't say there are just edge case there are simply lots of cases where ORM's don't fit.
David Rabinowitz replied on Fri, 2011/02/11 - 3:56am
Depends on the scope of the application and the exact nature of the sql. For complex model and dependency management it is worth the effort to invest in an ORM, but if you have simple table (or several) with simple flows then why to put ORM and its adjoining complexity? In many cases this code also reside in its own project (let's say a war that provides some services) and for me there is no reason to increase its size just for the sake of ORM.
Using Spring and JdbcTemplate (which is what we do), takes almost all the pain if handling connections, statements and all the preparation and clean up which are the main problem of using jdbc.
Dimitris Menounos replied on Fri, 2011/02/11 - 4:13am
Mladen Girazovski replied on Fri, 2011/02/11 - 5:13am
The reasons why people fail with frameworks are usually always the same: They do not read the documentation well enough and /or lack the theoretical background, then they jump straight to the implementation, usually without automated tests, so they see the wrong results just when they thought they were almost done.
ORMs are complex by nature, and always will be.
Of course, i'd prefer a working application with "plain" JDBC over a non-working (buggy) application with an ORM, but the cause is not the ORM or any framework as such, but the lack of skills in the development team.
Pick your frameworks wisely, based on evalution and the skill level/experience of the team, not on marketing. The choices made that have lots of consequences are also known to be part of the architecture.
This is not just the case with ORM, but also applies to WebFrameworks, build tools, DI Frameworks, etc. pp.
Some frameworks have their own set of problems, like EJB 2.x and JSF 1.x.
For most green field projects i use an ORM, but using iBatis/MyBatis for brown field projects with an existing schema often has advantages ime.
Jan Kotek replied on Fri, 2011/02/11 - 6:07am
Andy Leung replied on Fri, 2011/02/11 - 10:01am
Yes, it worked. BUT, it crashed.
I thought the story ended when I tested so many times, end-to-end and UAT. Everything went well. But after the weekend, users reported "no response" from front end on Monday. I spent so much time on investigating it and finally I found that it was a Hibernate memory leak issue. Weird, isn't it? The problem was, when there were requests coming continuously, the memory were reused and so server and hibernate were considered active. However, when there were no requests coming in for certain amount of time, the JDBC pool was trying to shrink down to minimum pool size and that triggered the memory unable to release error in Hibernate because it had references to it. It was tracked as major defect in major release of 3.2.4 or something. That was a very bad one!
Roger Marin replied on Fri, 2011/02/11 - 4:48pm
In my experience, iBatis(now MyBatis) is a great alternative to Hibernate/JPA when you don't want to go the JDBC route. I've used it with great success in several projects in the past, you should give it a try if you haven't.
Liam Knox replied on Fri, 2011/02/11 - 6:33pm
in response to:
Dimitris Menounos
However, there are fundamentals in evolution at work here. Would I trust building my application on Spring, written by experts, used by millions in countless applications world wide, or on your proprietary implementation?
Should we always all have knowledge of lower level details? At some point you need to build upon something you don't have full understanding of and can trust. For example what do you know about hardware design? Does it really matter relative to what you know at the higher level?
The abstraction point is very important here. Spring JDBC provides what JDBC does but at a higher abstraction by hiding boiler plate, and therefore should always be favoured. ORM's address different concerns so cannot be taken as a straight form of substitution for JDBC.
David Lee replied on Sat, 2011/02/12 - 11:00pm
For those claiming ORMs aren't the problem but people not reading the documentation is, please stop. Most projects simply don't benefit from an ORM, not matter how well you know said ORM. Period. And that's why people still write JDBC code and SQL. I would suggest to most, to give groovy a look. It pretty much can eliminate most of your broilerplate JDBC code. I've moved from iBatis to Groovy for SQL and I love it. Personally, I've always hated hibernate(maven and spring are on my hate list too). The author should do a post titled "Why are you using an ORM, when you could use iBatis or Groovy".
Igor Polevoy replied on Sun, 2011/02/13 - 3:27am
I could not agree more with Jilles van Gurp, all of his points are valid and resonate with my experience as well. What is interesting, I have learned Ruby on Rails a few years back, and was thoroughly impressed by ActiveRecord - this is a Ruby ORM for RoR. Experience using it was a refresher after heavy handed frameworks in Java, so much so, that after waiting for some time for someone to implement this in Java, I decided to do this myself. If anyone interested in a truly lightweight Java ORM, you can try ActiveJDBC. I will be publishing an introductory article on DZone soon. Some initial blogs can be found here: http://igorpolevoy.blogspot.com/2010/02/activerecord-in-java-activejdbc.html and here: http://java.productiveedge.com/
I tried to strike a good balance with the API simplicity, performance of the ORM layer, and especially making it as intuitive as possible.
enjoy
igor
Robin Bygrave replied on Sun, 2011/02/13 - 7:06pm
For people looking for a simplier alternative to Hibernate/JPA they could have a look at Ebean.
The main simplification is that is uses a "session-less" API - so the developer doesn't need to worry about attachment/detachment and the lifecycle of the Hibernate session (or JPA EntityManager).
It has a few other interesting features such as "Autofetch" which is automatic query tuning based on profiling of your application.
Anyway, just thought I'd mention it here for those people looking for a simplier alternative.
Eric Giese replied on Mon, 2011/02/14 - 5:40am
Jessie Mear replied on Wed, 2011/09/07 - 8:19am