How to get around a JDBC connection without using Spring / JPA / Hibernate

We have a Java J2EE application that uses individual web service calls for each insert / update of a database row. It turned out to be too slow. They made me fix it quickly. I plan to convert all web service calls to regular JDBC. To do this, I need to get the JDBC connection from the pool, and then use it in several ways. I need to use the same JDBC connection in multiple DAOs to merge all this into a single database transaction. I can explicitly pass the JDBC connection to each DAO that it needs, but it will require me to replace the LOT method signatures, as well as a lot of unit tests (which runs counter to the "fast" part).

I'm trying to come up with a good way to connect JDBC, and then just capture it in the methods that it needs, without having to explicitly pass it everywhere. We cannot use Spring, JPA or Hibernate in this project because the support team will not support these technologies. I can connect a JDBC connection to EJB, but I'm not sure how reliable this will be. I can create a custom Singleton to manage database connections for each user (session?), But I need to be careful about thread safety. If someone tried to do something like this before, I would appreciate some advice.

+5
source share
4 answers

ThreadLocal. , DAO

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();
    }
}

, , .

+1

Apache Commons DBCP. Pool Pool Apache , .

+2

(5 IBM WebSphere). jdbc- - sessionID. , ( sessionlistener). jdbc, . , . .

+1

Also, what I did for the DAO is not to pass the connection in the method signature, but in the constructor. Then I hold the object that initially creates the connection responsible for closing it.

0
source

All Articles