remove close() from database provider commit and rollback in transact
This commit is contained in:
parent
8baa0b608b
commit
810099b8c5
2 changed files with 94 additions and 6 deletions
|
@ -56,7 +56,6 @@ public final class DatabaseProvider implements Supplier<Database> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configure the database from the following properties read from the provided configuration:
|
* Configure the database from the following properties read from the provided configuration:
|
||||||
* <br/>
|
|
||||||
* <pre>
|
* <pre>
|
||||||
* database.url=... Database connect string (required)
|
* database.url=... Database connect string (required)
|
||||||
* database.user=... Authenticate as this user (optional if provided in url)
|
* 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) {
|
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;
|
boolean complete = false;
|
||||||
try {
|
try {
|
||||||
code.run(this);
|
code.run(this);
|
||||||
|
@ -677,6 +694,26 @@ public final class DatabaseProvider implements Supplier<Database> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T transactReturning(final DbCodeTyped<T> code) {
|
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;
|
T result;
|
||||||
boolean complete = false;
|
boolean complete = false;
|
||||||
try {
|
try {
|
||||||
|
@ -697,6 +734,27 @@ public final class DatabaseProvider implements Supplier<Database> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void transact(final DbCodeTx code) {
|
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();
|
Transaction tx = new TransactionImpl();
|
||||||
tx.setRollbackOnError(true);
|
tx.setRollbackOnError(true);
|
||||||
tx.setRollbackOnly(false);
|
tx.setRollbackOnly(false);
|
||||||
|
@ -721,15 +779,12 @@ public final class DatabaseProvider implements Supplier<Database> {
|
||||||
if (delegateTo != null) {
|
if (delegateTo != null) {
|
||||||
return delegateTo.get();
|
return delegateTo.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (database != null) {
|
if (database != null) {
|
||||||
return database;
|
return database;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (connectionProvider == null) {
|
if (connectionProvider == null) {
|
||||||
throw new DatabaseException("Called get() on a DatabaseProvider after close()");
|
throw new DatabaseException("Called get() on a DatabaseProvider after close()");
|
||||||
}
|
}
|
||||||
|
|
||||||
Metric metric = new Metric(log.isLoggable(Level.FINE));
|
Metric metric = new Metric(log.isLoggable(Level.FINE));
|
||||||
try {
|
try {
|
||||||
connection = connectionProvider.get();
|
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() {
|
public void commitAndClose() {
|
||||||
if (delegateTo != null) {
|
if (delegateTo != null) {
|
||||||
log.fine("Ignoring commitAndClose() because this is a fake provider");
|
log.fine("Ignoring commitAndClose() because this is a fake provider");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (connection != null) {
|
if (connection != null) {
|
||||||
try {
|
try {
|
||||||
if (!options.flavor().autoCommitOnly()) {
|
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() {
|
public void rollbackAndClose() {
|
||||||
if (delegateTo != null) {
|
if (delegateTo != null) {
|
||||||
log.fine("Ignoring rollbackAndClose() because this is a fake provider");
|
log.fine("Ignoring rollbackAndClose() because this is a fake provider");
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package org.xbib.jdbc.query.test;
|
package org.xbib.jdbc.query.test;
|
||||||
|
|
||||||
|
import java.util.logging.Level;
|
||||||
import org.junit.jupiter.api.Disabled;
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.xbib.jdbc.query.DatabaseProvider;
|
import org.xbib.jdbc.query.DatabaseProvider;
|
||||||
|
@ -114,7 +115,7 @@ public class DerbyTest extends CommonTest {
|
||||||
new Schema().addTable("dbtest").addColumn("i").asBigDecimal(31, 31).schema().execute(db);
|
new Schema().addTable("dbtest").addColumn("i").asBigDecimal(31, 31).schema().execute(db);
|
||||||
BigDecimal value = new BigDecimal("0.9999999999999999999999999999999"); // 31 digits
|
BigDecimal value = new BigDecimal("0.9999999999999999999999999999999"); // 31 digits
|
||||||
db.toInsert("insert into dbtest (i) values (?)").argBigDecimal(value).insert(1);
|
db.toInsert("insert into dbtest (i) values (?)").argBigDecimal(value).insert(1);
|
||||||
System.out.println(db.toSelect("select i from dbtest").queryBigDecimalOrNull());
|
logger.log(Level.INFO, db.toSelect("select i from dbtest").queryBigDecimalOrNull().toString());
|
||||||
assertEquals(value,
|
assertEquals(value,
|
||||||
db.toSelect("select i from dbtest where i=?").argBigDecimal(value).queryBigDecimalOrNull());
|
db.toSelect("select i from dbtest where i=?").argBigDecimal(value).queryBigDecimalOrNull());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue