humkins
humkins

Reputation: 10667

Must these short methods be synchronized in Java?

Consider following static helper

public class DbUtil {

    private static final Logger logger = LogManager.getLogger(DbUtil.class);

    public static void closeAll(ResultSet rs, Statement stmt, Connection conn) {
        close(rs);
        close(stmt);
        close(conn);
    }

    public static void close(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                logger.error(null, e);
            }
        }
    }

    public static void close(Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                logger.error(null, e);
            }
        }
    }

    public static void close(ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                logger.error(null, e);
            }
        }
    }

    public static void closeAny(Object o) {
        if(o == null) return;

        try {
            if (o instanceof Statement) {
                Statement stmt = (Statement) o;
                stmt.close();
            } else if(o instanceof Connection) {
                Connection conn = (Connection) o;
                conn.close();
            } else if(o instanceof ResultSet) {
                ResultSet rs = (ResultSet) o;
                rs.close();
            } else {
                logger.warn("Unknown object type");
            }
        } catch(SQLException e) {
            logger.error(null, e);
        }
    }

}

This class can be used by multiple thread. Must all its methods be synchronized or some of them and why?

Actually I'm not sure is it possible that after resultSet != null check by one thread formal parameter(s) are changed with different object instance(s) by another thread and thus another ResultSet, Statement or Connection is closed (in case the methods are not synchronized).

Upvotes: 0

Views: 114

Answers (2)

Marko Topolnik
Marko Topolnik

Reputation: 200138

I'm not sure is it possible that after resultSet != null check by one thread formal parameter(s) are changed with different object instance(s) by another thread

No, method arguments have the same semantics as local variables. They are private to a single method invocation and cannot be mutated by any other thread.

If each of your calling threads uses objects from its own thread-local connection, then there are no issues. Your methods do not share any state in that case.

Upvotes: 2

Crazyjavahacking
Crazyjavahacking

Reputation: 9677

Starting from Java 7 you should use much easier code:

public class DbUtil {

    private static final Logger logger = LogManager.getLogger(DbUtil.class);

    public static void closeAll(AutoCloseable... resources) {
        for (AutoCloseable resource: resources)
            close(resource);
    }

    public static void close(AutoCloseable resource) {
        if (resource != null) {
            try {
                resource.close();
            } catch (Exception e) {
                logger.error(null, e);
            }
        }
    }
}

Upvotes: 1

Related Questions