Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass around JDBC connection without using Spring/JPA/Hibernate

We have a Java J2EE application that was using individual web service calls for each database row insert/update. That turned out to be WAY too slow. They have brought me in to "quickly" fix it. I plan to convert all the web service calls to plain JDBC. To do that, I need to get a JDBC connection from the pool and then use it in multiple different methods. I need to use the same JDBC connection in multiple DAOs to string it all together into a single database transaction. I can explicitly pass around the JDBC connection to each DAO that needs it, but that would require me to change a LOT of method signatures, plus a LOT of unit tests (which goes against the "quickly" part).

I am trying to come up with a good way put the JDBC connection somewhere and then just grab it in the methods that need it without having to explicitly pass it around everywhere. We can't use Spring, JPA, or Hibernate on this project because the support team won't support those technologies. I can put the JDBC connection into an EJB, but I am not sure how reliable that would be. I could create a custom Singleton to manage database connections for each user (session?), but I would have to be careful about thread safety. If anyone has tried to do something like this before, I would appreciate some advice.

like image 878
Shane Avatar asked Dec 09 '10 16:12

Shane


People also ask

Is JPA better than JDBC?

Also, JPA is thought to be better suited for more sophisticated applications by many developers. But, JDBC is considered the preferable alternative if an application will use a simple database and we don't plan to migrate it to a different database vendor.

Which is better Spring JDBC or Spring JPA?

Spring Data JDBC has less abstractions than Spring Data JPA, but uses Spring Data concepts to make it easier to do CRUD operations than Spring JDBC. It sits closer to the database because it does not contain the most part of the Spring Data magic when querying the database.

What is difference between JDBC and Hibernate and JPA?

Hibernate is an implementation of JPA(Java Persistence API). 1. In JDBC, one needs to write code to map the object model's data representation to the schema of the relational model. Hibernate maps the object model's data to the schema of the database itself with the help of annotations.

What happens if you dont close JDBC connection?

If you don't close it, it leaks, and ties up server resources. @EJP The connection itself might be thread-safe (required by JDBC), but the applications use of the connection is probably not threadsafe. Think of things like different transaction isolation, boundaries (commit/rollback/autocommit) etc.


2 Answers

You could use a ThreadLocal. Have the entry point set it up and than the DAOs

class ConnectionUtil {
    public static final ThreadLocal<Connection> connection = new ThreadLocal<Connection>();
}

public Return method(Args arg) {
    ConnectionUtil.connection.set(newConnection());
    try {
        ...
    } finally {
        ConnectionUtil.connection.remove();
    }
}

Pretty ugly, but that seems to be what your boss wants.

like image 66
sblundy Avatar answered Oct 05 '22 19:10

sblundy


Use Apache Commons DBCP. It's the Connection Pool project from Apache, and what's used internally in many engines.

like image 32
Valentin Rocher Avatar answered Oct 05 '22 19:10

Valentin Rocher