添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The following code gives an OBL_UNSATISFIED_OBLIGATION warning:

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
public class Sql {
	private final DataSource dataSource = null;
	public void executeStatement(String statementString, List<?> arguments) throws SQLException {
		try (Connection connection = dataSource.getConnection()) {
			try (PreparedStatement statement = connection.prepareStatement(statementString)) {
				statement.setFetchSize(50);
				for (int i = 0; i < arguments.size(); i++) {
					statement.setObject(i + 1, arguments.get(i));
				try (ResultSet resultSet = statement.executeQuery()) {
					System.out.println(resultSet);

I think this can be regarded as a false-positive.

Another example of a false OBL_UNSATISFIED_OBLIGATION bug:

51  public void execute(final String query) {
52    try (Connection conn = pool.getDatabaseConnection();
53            Statement stmt = conn.createStatement();) {
54      stmt.execute(query);
55    } catch (SQLException ex) {
56      logSQLException(ex);
57    }
58  }

generates:

[INFO] com.mycompany.db.Database.execute(String) may fail to clean up java.sql.Statement [com.mycompany.db.Database, com.mycompany.db.Database, com.mycompany.db.Database, com.mycompany.db.Database, com.mycompany.db.Database, com.mycompany.db.Database] Obligation to clean up resource created at Database.java:[line 53] is not dischargedPath continues at Database.java:[line 52]Path continues at Database.java:[line 54]Path continues at Database.java:[line 55]Path continues at Database.java:[line 57]Path continues at Database.java:[line 58] OBL_UNSATISFIED_OBLIGATION
          

I'm also seeing this on Java 8 with code like:

public static MyObject forId(long myObjectId, Connection cxn) throws SQLException {
    try (PreparedStatement stmt = cxn.prepareStatement(selectMyObjectById))) {
        stmt.setLong(1, myObjectId);
        try (ResultSet res = stmt.executeQuery()) {
            res.next();
            return MyObject.fromResultSet(res);

where the JDBC Connection object is passed (but created with try-with-resources itself).

Is there any hope for this? I hate to scatter annotations to ignore this but I'd like to use the resource leak detector.