简体   繁体   中英

Proper way to handle JDBC connection in EJB 3 (SLSB)

I ask this question especially for Stateless Session Bean. I knew that I can easily inject the DataSource with the @Resource annotation. But I don't know what is the proper way to get the Connection . Is it in each method of the bean, or in the method annotated with @PostConstruct ? And also for the closing of the Connection . Do I have to close it within the finally block in each method call, or in the method annotated with @PreDestroy ?

Is it safe to create an instance variable for the Connection , for example:

@Stateless
public class MyBean {
    @Resource private DataSource ds;
    private Connection conn;

    @PostConstruct
    public void onCreate() {
        conn = ds.getConnection();    // within try catch block
    }

    @PreDestroy
    public void onDestroy() {
        conn.close()    // within try catch block
    }
}

Or should I create them locally in each method like this:

@Stateless
public class MyBean {
    @Resource private DataSource ds;

    public void method1() {
        Connection conn = null;
        // get and close connection...
    }

    public void method2() {
        Connection conn = null;
        // get and close connection...
    }
}

Some people in the Internet do this way, and some other do that way. What is the proper method to be implemented in an application with a high request traffic? When the bean instance is returned back to the EJB pool, does the Connection remains opened or does it returned to the database pool?

Note: The application is using native JDBC API. There are no JPA, JDO, etc.. The application server is Wildfly.

TL;DR The second approach is the correct one. Just make sure to close the connection to return it to the Pool.

The Datasource is a pool of connections, every time you get a connection it borrows one from the datasource and when you close that connection it will be returned to the pool, so you will always want to release the connection as soon as possible.

In the first approach you will retain the connection for as long as the EJB lives in memory. Since the EJB is an Stateless bean it will be alive for long and reused by diferent consumenrs. Making you have at least 1 connection open per EJB that is alive thus this approach is not practical.

The second approach is the correct one. Just make sure to close the connection to return it to the Pool. With this approach the Bean will only retain the connection while in use. Just make sure to close the connection to return it to the Pool.

@Stateless
public class MyBean {
  @Resource private DataSource ds;

  public void method1() {
    try(Connection conn = ds.getConnection()){
      // Do anything you need with the connection
    }
  }

  public void method2() {
    Connection conn = ds.getConnection();
    try {
      // Do anything you need with the connection
    } finally {
      connection.close();
    }
  }
}

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