Skip to content

Commit

Permalink
Added a test to show that XAER_RMERR is returned from the postgres dr…
Browse files Browse the repository at this point in the history
…iver on the second call to rollback
  • Loading branch information
tomjenkinson committed Jul 30, 2013
0 parents commit 944d45e
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
39 changes: 39 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- JBoss, Home of Professional Open Source Copyright 2008, Red Hat Middleware
LLC, and others contributors as indicated by the @authors tag. All rights
reserved. See the copyright.txt in the distribution for a full listing of
individual contributors. This copyrighted material is made available to anyone
wishing to use, modify, copy, or redistribute it subject to the terms and
conditions of the GNU Lesser General Public License, v. 2.1. This program
is distributed in the hope that it will be useful, but WITHOUT A WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU Lesser General Public License for more details. You
should have received a copy of the GNU Lesser General Public License, v.2.1
along with this distribution; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<groupId>com.tomjenkinson</groupId>
<version>0.0.1-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<artifactId>xa-recovery</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.jboss.spec.javax.transaction</groupId>
<artifactId>jboss-transaction-api_1.2_spec</artifactId>
<version>1.0.0.Alpha3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</project>
96 changes: 96 additions & 0 deletions src/main/java/RecoverDatasource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import static org.junit.Assert.assertTrue;

import java.sql.SQLException;

import javax.sql.XAConnection;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;

import org.junit.Test;
import org.postgresql.xa.PGXADataSource;

public class RecoverDatasource {

@Test
public void recover() throws SQLException, XAException {
String hostName = "localhost";
String userName = "sa";
String password = "sa";
String recoveryUserName = "sa";
String recoveryPassword = "sa";
String databaseName = "QUICKSTART_DATABASENAME";
int port = 5432;

{
PGXADataSource dataSource = new PGXADataSource();
dataSource.setPortNumber(port);
dataSource.setUser(userName);
dataSource.setPassword(password);
dataSource.setServerName(hostName);
dataSource.setDatabaseName(databaseName);

XAConnection xaConnection = dataSource.getXAConnection();
XAResource xaResource2 = xaConnection.getXAResource();
Xid xid = new Xid() {

public byte[] getBranchQualifier() {
return "branch".getBytes();
}

public int getFormatId() {
return 123456;
}

public byte[] getGlobalTransactionId() {
return "gtrid".getBytes();
}
};

xaResource2.start(xid, XAResource.TMNOFLAGS);
xaConnection.getConnection().createStatement()
.execute("INSERT INTO foo (bar) VALUES (1)");
xaResource2.end(xid, XAResource.TMSUCCESS);
xaResource2.prepare(xid);

}

{
PGXADataSource dataSource = new PGXADataSource();
dataSource.setPortNumber(port);
dataSource.setUser(recoveryUserName);
dataSource.setPassword(recoveryPassword);
dataSource.setServerName(hostName);
dataSource.setDatabaseName(databaseName);

XAResource xaResource = dataSource.getXAConnection()
.getXAResource();
Xid[] recover = xaResource.recover(XAResource.TMSTARTRSCAN);
int rolledBack = 0;
for (int i = 0; i < recover.length; i++) {
try {
System.out.println("Rolling back: " + recover[i]);
xaResource.rollback(recover[i]);
System.out.println("Rolled back");
rolledBack++;

xaResource.rollback(recover[i]);
System.out.println("Rolled back");
rolledBack++;
} catch (XAException e) {
if (e.errorCode == XAException.XAER_RMERR) {
System.err
.println("Receiced XAER_RMERR rather than XAER_NOTA");
}
System.err.println(e.errorCode);
e.printStackTrace();
}
}
xaResource.recover(XAResource.TMENDRSCAN);
int expected = 1;
if (expected >= 0) {
assertTrue("Rolled back: " + rolledBack, rolledBack == expected);
}
}
}
}

0 comments on commit 944d45e

Please sign in to comment.