[concurrency-interest] reordering
Bart Jacobs
bart.jacobs at cs.kuleuven.be
Fri May 19 07:44:31 EDT 2006
There are multiple ways to explain the Java memory model. One uses the
notion of "reorderings".
There is also a different explanation, which is not in terms of
"reorderings", and which may be clarifying for you. In this other
explanation, in each program execution, all actions performed by a given
thread occur in the order specified by the program (known as the program
order). The only difference with the single-threaded semantics is that,
for fields accessed concurrently by multiple threads, field reads do not
necessarily yield the value written by the most recent preceding write.
In short, the memory model has no effect on single-threaded programs,
and it affects only fields accessed concurrently by multiple threads.
Bart
Kazimierz Pogoda wrote:
> 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?
>
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
More information about the Concurrency-interest
mailing list