Subject Re: [Firebird-Java] Desktop application -- two phase commit without application server--
Author Jan Hubeny
Adriano dos Santos Fernandes napsal(a):
>
> Jan Hubeny escreveu:
> >
> > Could you please give me advice how to implement the two phase commit
> > correctly?
> >
> There is an example on TestFBXAResource.testDo2XATrans() in Jaybird
> sources...
>
> .
> This example shows two one phase commits over one database. I need to
> perform two phase commit over two databases.
I think that I rather should use the skeleton from
TestXADataSource.testXAConnection() method.
However, I am confused by exception handling and start-> end -> prepare
--> commit | rollback execution chain.

1) I don't know when to catch the SQLExceptions throwed by
stmtA.execute() and stmtB.execute() method calls.
If I catch them in their own try catch block, the method calls
xaResA.prepare(xidA);
xaResB.prepare(xidB);
did not indicate that the whole global transaction should be rollback.

If I don't modify the try finally block of stmtA.execute() and
stmtB.execute(), and the stmt execution throws exception, then no
changes to the both databases are committed. This is, correct.
However I don't know whether it is correct to start the xaResA and
xaResB and don't end them with the end -> prepare commit | rollback
chain. This chain is skipped when the SQLexception from stmt part is
thrown.


2) I am not sure how the start-> end -> prepare --> commit | rollback
execution chain should work. I don't know how to correctly build the try
catch block for each
of start,end prepare commit rollback methods. If I have the simple
one-phase commit then I can use following skeleton

Connection conn = null;
try {
conn = dataSource.getConnection();
..... //statement execution
conn.commit();
} catch (SQLException ex) {
if (conn != null) {
try {
conn.rollback ();
} catch (SQLException ex) {
}
}
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
}
}
}

Can you provide me some similar skeleton for the two phase commit to two
databases?

Thank you,

Honza Hubeny