|
|
|
@ -56,7 +56,6 @@ public final class DatabaseProvider implements Supplier<Database> {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Configure the database from the following properties read from the provided configuration:
|
|
|
|
|
* <br/>
|
|
|
|
|
* <pre>
|
|
|
|
|
* database.url=... Database connect string (required)
|
|
|
|
|
* database.user=... Authenticate as this user (optional if provided in url)
|
|
|
|
@ -659,6 +658,24 @@ public final class DatabaseProvider implements Supplier<Database> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void transact(final DbCode code) {
|
|
|
|
|
boolean complete = false;
|
|
|
|
|
try {
|
|
|
|
|
code.run(this);
|
|
|
|
|
complete = true;
|
|
|
|
|
} catch (ThreadDeath | DatabaseException t) {
|
|
|
|
|
throw t;
|
|
|
|
|
} catch (Throwable t) {
|
|
|
|
|
throw new DatabaseException("Error during transaction", t);
|
|
|
|
|
} finally {
|
|
|
|
|
if (!complete) {
|
|
|
|
|
rollback();
|
|
|
|
|
} else {
|
|
|
|
|
commit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void transactAndClose(final DbCode code) {
|
|
|
|
|
boolean complete = false;
|
|
|
|
|
try {
|
|
|
|
|
code.run(this);
|
|
|
|
@ -677,6 +694,26 @@ public final class DatabaseProvider implements Supplier<Database> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public <T> T transactReturning(final DbCodeTyped<T> code) {
|
|
|
|
|
T result;
|
|
|
|
|
boolean complete = false;
|
|
|
|
|
try {
|
|
|
|
|
result = code.run(this);
|
|
|
|
|
complete = true;
|
|
|
|
|
} catch (ThreadDeath | DatabaseException t) {
|
|
|
|
|
throw t;
|
|
|
|
|
} catch (Throwable t) {
|
|
|
|
|
throw new DatabaseException("Error during transaction", t);
|
|
|
|
|
} finally {
|
|
|
|
|
if (!complete) {
|
|
|
|
|
rollback();
|
|
|
|
|
} else {
|
|
|
|
|
commit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public <T> T transactReturningAndClose(final DbCodeTyped<T> code) {
|
|
|
|
|
T result;
|
|
|
|
|
boolean complete = false;
|
|
|
|
|
try {
|
|
|
|
@ -697,6 +734,27 @@ public final class DatabaseProvider implements Supplier<Database> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void transact(final DbCodeTx code) {
|
|
|
|
|
Transaction tx = new TransactionImpl();
|
|
|
|
|
tx.setRollbackOnError(true);
|
|
|
|
|
tx.setRollbackOnly(false);
|
|
|
|
|
boolean complete = false;
|
|
|
|
|
try {
|
|
|
|
|
code.run(this, tx);
|
|
|
|
|
complete = true;
|
|
|
|
|
} catch (ThreadDeath | DatabaseException t) {
|
|
|
|
|
throw t;
|
|
|
|
|
} catch (Throwable t) {
|
|
|
|
|
throw new DatabaseException("Error during transaction", t);
|
|
|
|
|
} finally {
|
|
|
|
|
if ((!complete && tx.isRollbackOnError()) || tx.isRollbackOnly()) {
|
|
|
|
|
rollback();
|
|
|
|
|
} else {
|
|
|
|
|
commit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void transactAndClose(final DbCodeTx code) {
|
|
|
|
|
Transaction tx = new TransactionImpl();
|
|
|
|
|
tx.setRollbackOnError(true);
|
|
|
|
|
tx.setRollbackOnly(false);
|
|
|
|
@ -721,15 +779,12 @@ public final class DatabaseProvider implements Supplier<Database> {
|
|
|
|
|
if (delegateTo != null) {
|
|
|
|
|
return delegateTo.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (database != null) {
|
|
|
|
|
return database;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (connectionProvider == null) {
|
|
|
|
|
throw new DatabaseException("Called get() on a DatabaseProvider after close()");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Metric metric = new Metric(log.isLoggable(Level.FINE));
|
|
|
|
|
try {
|
|
|
|
|
connection = connectionProvider.get();
|
|
|
|
@ -824,12 +879,27 @@ public final class DatabaseProvider implements Supplier<Database> {
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void commit() {
|
|
|
|
|
if (delegateTo != null) {
|
|
|
|
|
log.fine("Ignoring commit() because this is a fake provider");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (connection != null) {
|
|
|
|
|
try {
|
|
|
|
|
if (!options.flavor().autoCommitOnly()) {
|
|
|
|
|
connection.commit();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new DatabaseException("Unable to commit the transaction", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void commitAndClose() {
|
|
|
|
|
if (delegateTo != null) {
|
|
|
|
|
log.fine("Ignoring commitAndClose() because this is a fake provider");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (connection != null) {
|
|
|
|
|
try {
|
|
|
|
|
if (!options.flavor().autoCommitOnly()) {
|
|
|
|
@ -842,6 +912,23 @@ public final class DatabaseProvider implements Supplier<Database> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void rollback() {
|
|
|
|
|
if (delegateTo != null) {
|
|
|
|
|
log.fine("Ignoring rollback() because this is a fake provider");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (connection != null) {
|
|
|
|
|
try {
|
|
|
|
|
if (!options.flavor().autoCommitOnly()) {
|
|
|
|
|
connection.rollback();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.log(Level.SEVERE, "Unable to rollback the transaction", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void rollbackAndClose() {
|
|
|
|
|
if (delegateTo != null) {
|
|
|
|
|
log.fine("Ignoring rollbackAndClose() because this is a fake provider");
|
|
|
|
|