简体   繁体   中英

Need help figuring out a lightweight Java EE framework

We have an old and big Java EE project, where at some places due to bad coding database connections has not been closed properly/or not cleaned up in catch/finally block.

We have limited our database connection pool to 100 connections. Sometimes it happens that the connection remains open and all the 100 connections are used, so the application gets hanged up. I'm trying to restructure this project, obviously I'll take care of this bad code when I get there, I'm wondering is there any lightweight Java EE framework which closes this opened db connection automatically without writing conn.close() or session.close().

Maybe something like Django where every db connection are closed at the end of every request/reposnse cycle.

I do know that I can use tools like p6spy and IronTrack SQL to look for statements that fail to close, but I'm more interested in frameworks as this project doesn't use any and I'm trying to integrate this project with a framework.

There is try-with-resource in Java 7 that may help. Take a look

http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

http://docs.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources.html

Here Java 7 Automatic Resource Management JDBC (try-with-resources statement) you can similar question.

Check for detecting connection leak. I am sure, you will find some tools for this.

I think you should go through a couple different frameworks, demos should be available if you search for them and choose the one which most fits your current and near future needs. I personally like Primefaces/Hibernate (if you're in JSF).

The lightweight approach in Java EE is to use simple POJO based beans called EJBs that do the DB work.

With them, in many or all cases, the DB connection is a thing that's completely handled for you behind the covers by the server.

For instance (assuming your queries for now are native SQL):

@Stateless
public class MyBean {

    @PersistenceContext
    private EntityManager entityManager;

    public void doDBWork() {
        entityManager.createNativeQuery("update foo set a = 1").executeUpdate();
    }
}

In this example when you call doDBWork a transaction automatically starts and it ends when you leave that method. Somewhere in between a connection is retrieved from the connection pool and returned to it. The code is automatically totally safe in the face of exceptions or concurrent access.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM