Closing a Database Connection


EJB design: Closing a Database Connection

  1. Closing a Database Connection (4 messages)

    I have a stateless session beans that request a bunch of datasource connections from a pool (JBOSS/Minerva).

    Do I have to close the connections I requested explicitly? I seem to recall that I am not allowed to close the connection or in anyway manipulate the autocommit flag.

    However, if I don't close the connections, soon enough, I'd use up all connections in the pool and with Minerva, either my EJB gets blocked or it has to be able to handle a failure condition.

    So, my question is, do I have to close the connections I look up through JNDI and datasource.getConnection() explicitly? Thanks.

    Threaded Messages (4)

  2. Closing a Database Connection[ Go to top ]

    There are different ways to keep the db connections,
    in your case, you probably need to do CloseConn() more
    frequently, what it will do is returning the connection
    to the connection pool not really closing..
    And keep your transactions short, or you won't be able
    to free the connection.
  3. Closing a Database Connection[ Go to top ]

    The problem is that each of my transaction involve multiple connections. So you are saying that doing this in my stateless session bean:

    dataSource = initialCtx.lookup("blablaba/datasource");
    Connection conn = dataSource.getConnection();

    does not violate EJB spec and create problems with container managed transactions for stateless session beans?

    I know that doing this conn.setAutoCommit(true) is supposedly a very bad thing to do in a container managed transaction scenario.

  4. Closing a Database Connection[ Go to top ]

    It is ok and leave the job to the app server.
    The problem if you have a long span transaction,you will still hold the connection,if the app server is smart
    enought it should reuse the same connection for the
    same transaction.
  5. In container managed transactions, you should not call Connection methods commit, rollback and setAutoCommit(true).

    However,as qing said,you should close it as soon as you are done with it so that it can be reused.