refactor(export-import): clearer logging in case of failure
MODIFY
manager/api/core/src/main/java/io/apiman/manager/api/core/IStorage.java
manager/api/core/src/main/java/io/apiman/manager/api/core/IStorage.java
@@ -58,11 +58,10 @@ public interface IStorage {
/*
* Transaction related methods
*/
- //
- // public void beginTx() throws StorageException;
- // public void commitTx() throws StorageException;
- // public void rollbackTx();
- // public void initialize();
+
+ public void beginTx() throws StorageException;
+ public void commitTx() throws StorageException;
+ public void rollbackTx();
/*
* Various creation methods. These are called by the REST layer to create stuff.

MODIFY
manager/api/jpa/src/main/java/io/apiman/manager/api/jpa/JpaStorage.java
manager/api/jpa/src/main/java/io/apiman/manager/api/jpa/JpaStorage.java
@@ -219,6 +219,23 @@ public class JpaStorage extends AbstractJpaStorage implements IStorage, IStorage
super.create(plugin);
}
+ @Override
+ public void beginTx() throws StorageException {
+ if (!getSession().getTransaction().isActive()) {
+ getSession().beginTransaction();
+ }
+ }
+
+ @Override
+ public void commitTx() throws StorageException {
+ getSession().getTransaction().commit();
+ }
+
+ @Override
+ public void rollbackTx() {
+ getSession().getTransaction().rollback();
+ }
+
/**
* {@inheritDoc}
*/

MODIFY
manager/api/rest-impl/src/main/java/io/apiman/manager/api/rest/impl/SystemResourceImpl.java
manager/api/rest-impl/src/main/java/io/apiman/manager/api/rest/impl/SystemResourceImpl.java
@@ -182,7 +182,6 @@ public class SystemResourceImpl implements ISystemResource {
// the HTTP response output stream.
StreamingOutput stream = new StreamingOutput() {
@Override
- @Transactional
public void write(final OutputStream output) throws IOException, WebApplicationException {
final PrintWriter writer = new PrintWriter(output);
IApimanLogger logger = new IApimanLogger() {
@@ -251,7 +250,19 @@ public class SystemResourceImpl implements ISystemResource {
};
// Make sure we call through a managed service as the other `Stream` takes us out the managed context
// which can mess with our CDI interceptors.
+ try {
+ storage.beginTx();
importExportService.fullImport(importFile, logger);
+ storage.commitTx();
+ } catch (Exception e) {
+ logger.error(e);
+ storage.rollbackTx();
+ throw new IOException(e);
+ } finally {
+ writer.flush();
+ writer.close();
+ output.close();
+ }
}
};

MODIFY
manager/test/api/src/main/java/io/apiman/manager/test/server/JettyTransactionalInterceptor.java
manager/test/api/src/main/java/io/apiman/manager/test/server/JettyTransactionalInterceptor.java
@@ -63,7 +63,9 @@ public class JettyTransactionalInterceptor implements DataAccessUtilMixin {
if (!tx.getRollbackOnly()) {
// em.flush();
System.out.println("commit");
+ if (tx.isActive()) {
tx.commit();
+ }
} else {
tx.rollback();
}

refactor(export-import): clearer logging in case of failure