質問

I am following the EJB standard for CMP as specified in the specification but it does not rollback changes in a database. When I have commented-out Connection.close() (Connection is retrieved from a Data-source) it is rollbacked successfully.

Is it recommended for WebLogic to not close a connection received from a data source?

役に立ちましたか?

解決

Is it recommended for WebLogic to not close a connection received from a data source?

There is a rule that when inside a container managed transaction you should never call any method that manually or natively interacts with a transaction on a transactional resource.

But Connection.close() is not such a method. Even though connections are managed, when you obtain one from an injected data source, you indeed have to close it. Note that this will in most cases not actually close the connection, but with a transaction in progress the transaction manager will most likely keep the connection on hold to be able to do the commit or rollback on it when the overall transaction commits resp. rollbacks.

Note that a connection can be closed automatically when using a try-with-resources construct. Otherwise you indeed have to call the close() method on it (wrapped in some amount of finally clauses).

This is a fairly standard pattern:

@Stateless
public class MyEJB {

    @Resource(lookup = "java:app/dataSource")
    private DataSource dataSource;

    public void doStuff() {
        try (Connection connection = dataSource.getConnection()) {
            // do work on connection
        } catch (SQLException e) {
            // handle exception
        }
    }
}

See this link for some more discussion on this topic: http://www.coderanch.com/t/485357/EJB-JEE/java/releasing-connection-CMT

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top