トランザクション その8 例外を発生させずにRollbackしたい

今までトランザクション中に例外が発生すれば自動的にRollbackされることを確認してきましたが、例外が起きなくてもRollbackさせたい場合があるので、そのサンプルです。

Sample023.java
public static void main(String[] args) {
	String url = "jdbc:postgresql://192.168.52.128/jdbi";
	DBI dbi = new DBI(url, "jdbi_user", "jdbi_pass");
	final Handle otherHandler = dbi.open();
	final Handle h = dbi.open();

	h.insert("truncate table table001");

	System.out.println("before table001 rows count = " + countTable001(h)); // 0

	try {
		h.inTransaction(new TransactionCallback<Void>()
		{
			@Override
			public Void inTransaction(Handle handle, TransactionStatus status) throws Exception
			{
				System.out.println("[0] : count = " + countTable001(otherHandler)); // 0

				handle.insert("insert into table001(id,name) values(101, 'name101')");

				System.out.println("[1] : count = " + countTable001(otherHandler)); // 0

				handle.attach(Dao010.class).noTxSuccessTest();

				System.out.println("[2] : count = " + countTable001(otherHandler)); // 0

				status.setRollbackOnly();

				return null;
			}
		});
	} catch(TransactionFailedException e) {
		e.printStackTrace(); // 例外発生
	}

	System.out.println("after table001 rows count = " + countTable001(h)); // 0

	h.close();
	otherHandler.close();
}

public static int countTable001(final Handle h)
{
	return h.createQuery("select count(*) from table001").map(IntegerMapper.FIRST).first();
}

前回までとほとんど同じですが、「status.setRollbackOnly();」を呼ぶことにより、Rollbackしてくれるようになります。
ただ1点注意が必要で、トランザクション中に例外が発生したわけではないですけど、トランザクションが失敗した例外(TransactionFailedException)が発生するので、catchしないとアボーンしちゃいます。