mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1933 - ENH: Add DtoQuery.usingTransaction(Transaction) ... dto query with supplied transaction
This commit is contained in:
@@ -136,4 +136,8 @@ public interface DtoQuery<T> {
|
||||
*/
|
||||
DtoQuery<T> setBufferFetchSizeHint(int bufferFetchSizeHint);
|
||||
|
||||
/**
|
||||
* Use the explicit transaction to execute the query.
|
||||
*/
|
||||
DtoQuery<T> usingTransaction(Transaction transaction);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.ebeaninternal.api;
|
||||
|
||||
import io.ebean.DtoQuery;
|
||||
import io.ebean.ProfileLocation;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebeaninternal.server.dto.DtoMappingRequest;
|
||||
import io.ebeaninternal.server.dto.DtoQueryPlan;
|
||||
|
||||
@@ -65,4 +66,9 @@ public interface SpiDtoQuery<T> extends DtoQuery<T>, SpiSqlBinding {
|
||||
*/
|
||||
SpiQuery<?> getOrmQuery();
|
||||
|
||||
/**
|
||||
* Return the explicit transaction used to execute the query.
|
||||
*/
|
||||
Transaction getTransaction();
|
||||
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ public final class DtoQueryRequest<T> extends AbstractSqlQueryRequest {
|
||||
private DataReader dataReader;
|
||||
|
||||
DtoQueryRequest(SpiEbeanServer server, DtoQueryEngine engine, SpiDtoQuery<T> query) {
|
||||
super(server, query, null);
|
||||
super(server, query, query.getTransaction());
|
||||
this.queryEngine = engine;
|
||||
this.query = query;
|
||||
query.obtainLocation();
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.ebeaninternal.server.querydefn;
|
||||
|
||||
import io.ebean.DtoQuery;
|
||||
import io.ebean.ProfileLocation;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebeaninternal.api.BindParams;
|
||||
import io.ebeaninternal.api.SpiDtoQuery;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
@@ -47,6 +48,8 @@ public class DefaultDtoQuery<T> implements SpiDtoQuery<T> {
|
||||
*/
|
||||
private final BindParams bindParams = new BindParams();
|
||||
|
||||
private Transaction transaction;
|
||||
|
||||
/**
|
||||
* Create given an underlying ORM query.
|
||||
*/
|
||||
@@ -88,6 +91,12 @@ public class DefaultDtoQuery<T> implements SpiDtoQuery<T> {
|
||||
descriptor.putQueryPlan(planKey, plan);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DtoQuery<T> usingTransaction(Transaction transaction) {
|
||||
this.transaction = transaction;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void findEach(Consumer<T> consumer) {
|
||||
server.findDtoEach(this, consumer);
|
||||
@@ -148,6 +157,11 @@ public class DefaultDtoQuery<T> implements SpiDtoQuery<T> {
|
||||
return ormQuery;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Transaction getTransaction() {
|
||||
return transaction;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DtoQuery<T> setRelaxedMode() {
|
||||
this.relaxedMode = true;
|
||||
|
||||
@@ -14,6 +14,7 @@ import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TestQueryUsingConnection extends BaseTestCase {
|
||||
|
||||
@@ -51,6 +52,15 @@ public class TestQueryUsingConnection extends BaseTestCase {
|
||||
|
||||
DB.getDefault().insert(x, transaction);
|
||||
|
||||
final CountryDto dto = DB.findDto(CountryDto.class, "select code, name from o_country where code=?")
|
||||
.usingTransaction(transaction)
|
||||
.setParameter(1, "xx")
|
||||
.findOne();
|
||||
|
||||
assertThat(dto).isNotNull();
|
||||
assertEquals("xx", dto.code);
|
||||
assertEquals("WillRollThisBack", dto.name);
|
||||
|
||||
final int count = DB.find(Country.class)
|
||||
.usingTransaction(transaction)
|
||||
.findCount();
|
||||
@@ -63,4 +73,14 @@ public class TestQueryUsingConnection extends BaseTestCase {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class CountryDto {
|
||||
final String code;
|
||||
final String name;
|
||||
|
||||
public CountryDto(String code, String name) {
|
||||
this.code = code;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user