VOOZH about

URL: https://www.javacodegeeks.com/2014/02/jdbc-4-0s-lesser-known-clob-free-and-blob-free-methods.html

โ‡ฑ JDBC 4.0โ€™s Lesser-known Clob.free() and Blob.free() Methods


When I talk about jOOQ at conferences, I always show this slide containing a bunch of very common JDBC mistakes that people often commit:
 
 
 
 
 
 
 
 
 
 
๐Ÿ‘ error

Six common JDBC bugs in this image

Can you find the bugs? Some of them are obvious, such as:

  • Line 4: Syntax errors resulting from bad concatenation on line 3
  • Line 7: Syntax errors and SQL injection risk due to variable inlining
  • Line 8: Wrong bind index resulting from a potential mismatch on line 3
  • Line 14: Wrong column name due to sloppy rename
  • Line 18: Bad resource management

But then, thereโ€™s another very subtle bug that most people are unaware of because the fix was only possible since the upgrade in Java 6 / JDBC 4.0. See the solution, below:

๐Ÿ‘ solution

Solution to the previous six bugs

With JDBC 4.0, the Clob.free() and the Blob.free() methods were introduced. While calling them is optional, it may be a very bad idea not to call them as early as possible, as you should not rely on the garbage collector to kick in early enough to free these resources. In fact, in certain databases / JDBC drivers, LOBs can outlive individual statements and/or transactions. Theyโ€™re beasts of their own. If youโ€™re reading through the JDBC tutorial (and also in the JDBC specification), it says:

Blob, Clob, and NClob Java objects remain valid for at least the duration of the transaction in which they are created. This could potentially result in an application running out of resources during a long running transaction.

The same is true for arrays, which also have an Array.free() method since Java 6 / JDBC 4.0.

So if your application has long-running transactions, do call these free() methods, or make it a habit to always call them. Weโ€™ll file an issue to FindBugs to make this a potential bug pattern.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

Tags
JDBC
๐Ÿ‘ Photo of Lukas Eder
Lukas Eder
February 7th, 2014Last Updated: February 8th, 2014
1 108 1 minute read

Lukas Eder

Lukas is a Java and SQL enthusiast developer. He created the Data Geekery GmbH. He is the creator of jOOQ, a comprehensive SQL library for Java, and he is blogging mostly about these three topics: Java, SQL and jOOQ.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
12 years ago

Great info, didnโ€™t knew about these โ€œnewโ€ JDBC methods.

0
Reply
Back to top button
Close
wpDiscuz