I am already working 5 years on an application where I work a lot with files. Sometimes it is desired to have some transactional behaviour.
I think XA disk is pretty cool but it doesn't work like I had in mind. But perhaps I am doing something wrong orso ...
I don't think I need a XA session because I stay in the same JVM (as told on the website)
public class XaTransactionExample {
private static final String XA_NAMESPACE = "D:\\XADiskSystem1";
public static void main(String[] args) {
try {
new XaTransactionExample().execute();
} catch (Exception e) {
e.printStackTrace();
}
}
public void execute() throws Exception {
System.out.println("Creating XA-disk instance ...");
Session session = null;
TransactionManager transactionManager = null;
XAFileSystem fileSystem = null;
try {
StandaloneFileSystemConfiguration xaConfiguration = new StandaloneFileSystemConfiguration(XA_NAMESPACE, "1");
fileSystem = XAFileSystemProxy.bootNativeXAFileSystem(xaConfiguration);
System.out.println("Creating XA-disk file system ...");
fileSystem.waitForBootup(-1);
System.out.println("Successfully started up XA-disk instance ...");
System.out.println("Creating XA-disk transaction manager ...");
transactionManager = new UserTransactionManager();
transactionManager.begin();
System.out.println("XA-transaction has been started ...");
session = fileSystem.createSessionForLocalTransaction();
System.out.println("XA-session has been created ...");
System.out.println("Performing actions ...");
session.createFile(new File("D:\\xa\\test.txt"), false);
session.deleteFile(new File("D:\\xa\\THS - Disc Over.aif"));
session.commit(); //or transactionManager.commit(); same behaviour...
System.out.println("XA-transaction has been committed ...");
} catch (Exception ex) {
session.rollback();
System.out.println("XA-transaction has been rolled back ...");
ex.printStackTrace();
} finally {
if(fileSystem != null) {
System.out.println("XA-disk is shutting down ...");
fileSystem.shutdown();
}
}
}
}
The example above is almost identic to your example on your website. I am just playing with it before I integrate it in my application.
I have 2 files. 1 I want to create and 1 file should be deleted and this should be transactional. But the file that should be deleted is an audio file that is playing at that moment.
Actual behaviour: file is created and then timeout on delete ... no rollback of created file
Expeted behaviour: file is not created (rollback) and file is not deleted (rollback)
Thanks in advance.