[concurrency-interest] reordering

Kazimierz Pogoda morisil at ncdc.pl
Fri May 19 06:11:43 EDT 2006


This "reordering" idea made me suspicious about my own code. I know this
is not directly connected with concurrent programming, but I want to ask
experts, if this code fragment works as it is expected to:


public void execute(final String sql) throws SQLException {
	final Connection conn = getConnection(); //throws SQLException
	boolean executed = false;
	try {
		execute(conn, sql); //throws SQLException
		executed = true;
	} finally {
		boolean commited = false;
		if (executed) {
			try {
				conn.commit();
				commited = true;
			} catch (SQLException e) {
				log("cannot commit connection", e);
			}
		}
		if (!commited) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				log("cannot rollback connection", e);
			}
		}
		try {
			conn.close();
		} catch (SQLException e) {
			log("cannot close connection", e);
		}
	}
}

The intent of this code is to throw to caller the original SQLException
(thrown by execute(conn, sql) method), not SQLException indicating that
connection cannot be commited or closed. In case of any Throwable
(RuntimeException, Error) thrown in execute(conn, sql) method I expect
also an attempt to rollback the connection (If connection is taken from
the pool, other thread can commit it later).

I'm using boolean flags "executed" to indicate if Throwable has been
throw, and boolean flag "commited" for SQLException on commit().

Is it possible, that "reordering" will effect in unexpected state of
those flags after some thrown Throwable?

-- 
"the spirit can only withdraw into a corner. And yet, it is not at all 
 worn out or superfluous, but, like an (eternal) witness, floats above 
 the rubble of culture - almost like an  avenger of God.  As though it 
 awaited a new incarnation."
                                        -- Ludwig Wittgenstein --
Kazimierz Pogoda
mailto:morisil at ncdc.pl



More information about the Concurrency-interest mailing list