mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Added @Transactional support for PersistBatch #220
This commit is contained in:
@@ -3,6 +3,7 @@ package com.avaje.tests.batchinsert;
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.ebean.annotation.Transactional;
|
||||
import com.avaje.ebean.config.PersistBatch;
|
||||
import com.avaje.tests.model.basic.UTDetail;
|
||||
import com.avaje.tests.model.basic.UTMaster;
|
||||
@@ -29,7 +30,7 @@ public class TestBatchInsertSimple extends BaseTestCase {
|
||||
|
||||
for (int i = 0; i < numOfMasters; i++) {
|
||||
UTMaster master = createMasterAndDetails(i, 20);
|
||||
Ebean.save(master);
|
||||
master.save();
|
||||
}
|
||||
|
||||
transaction.commit();
|
||||
@@ -39,6 +40,24 @@ public class TestBatchInsertSimple extends BaseTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransactional() {
|
||||
|
||||
saveWithFullBatchMode();
|
||||
}
|
||||
|
||||
@Transactional(batch=PersistBatch.ALL, batchSize=50)
|
||||
public void saveWithFullBatchMode() {
|
||||
|
||||
int numOfMasters = 4;
|
||||
|
||||
for (int i = 0; i < numOfMasters; i++) {
|
||||
UTMaster master = createMasterAndDetails(i, 5);
|
||||
// the save is 'batched' and does not execute immediately
|
||||
// ... it now acts more like 'merge/persist'
|
||||
master.save();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJdbcBatchPerRequestWithMasterOnly() {
|
||||
@@ -132,8 +151,8 @@ public class TestBatchInsertSimple extends BaseTestCase {
|
||||
|
||||
UTDetail detail = new UTDetail();
|
||||
detail.setName("batchInsert-detail-" + position);
|
||||
detail.setQty(Integer.valueOf(qty));
|
||||
detail.setAmount(Double.valueOf(amount));
|
||||
detail.setQty(qty);
|
||||
detail.setAmount(amount);
|
||||
|
||||
// System.out.println("-- "+detail);
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.avaje.tests.model.basic;
|
||||
|
||||
import com.avaje.ebean.Model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -12,7 +14,7 @@ import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
@Table(name="ut_master")
|
||||
public class UTMaster {
|
||||
public class UTMaster extends Model {
|
||||
|
||||
@Id
|
||||
Integer id;
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.util.List;
|
||||
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
|
||||
import com.avaje.ebean.config.PersistBatch;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -12,6 +13,8 @@ import com.avaje.ebean.Transaction;
|
||||
import com.avaje.ebean.TxType;
|
||||
import com.avaje.ebean.annotation.Transactional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class DummyDao {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(DummyDao.class);
|
||||
@@ -33,5 +36,34 @@ public class DummyDao {
|
||||
public void addToObject(Long id, Double anotherNumber, List<Long> ids) throws EntityNotFoundException {
|
||||
// and more code
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Transactional(batch = PersistBatch.ALL, batchOnCascade = PersistBatch.ALL, batchSize = 99)
|
||||
public void doWithBatchOptionsSet() {
|
||||
|
||||
Transaction txn = Ebean.currentTransaction();
|
||||
assertEquals(PersistBatch.ALL, txn.getBatch());
|
||||
assertEquals(PersistBatch.ALL, txn.getBatchOnCascade());
|
||||
assertEquals(99, txn.getBatchSize());
|
||||
}
|
||||
|
||||
|
||||
@Transactional(batch = PersistBatch.INSERT, batchOnCascade = PersistBatch.NONE, batchSize = 77)
|
||||
public void doOuterWithBatchOptionsSet() {
|
||||
|
||||
Transaction txn = Ebean.currentTransaction();
|
||||
|
||||
assertEquals(PersistBatch.INSERT, txn.getBatch());
|
||||
assertEquals(PersistBatch.NONE, txn.getBatchOnCascade());
|
||||
assertEquals(77, txn.getBatchSize());
|
||||
|
||||
doWithBatchOptionsSet();
|
||||
|
||||
// batch options set back
|
||||
assertEquals(PersistBatch.INSERT, txn.getBatch());
|
||||
assertEquals(PersistBatch.NONE, txn.getBatchOnCascade());
|
||||
assertEquals(77, txn.getBatchSize());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,6 +18,15 @@ public class TestTxTypeOnTransactional extends BaseTestCase {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(TestTxTypeOnTransactional.class);
|
||||
|
||||
@Test
|
||||
public void testBatchOptionsAreSet() {
|
||||
|
||||
logger.info("-- test pre doOuterWithBatchOptionsSet");
|
||||
DummyDao dao = new DummyDao();
|
||||
dao.doOuterWithBatchOptionsSet();
|
||||
logger.info("-- test post doOuterWithBatchOptionsSet");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user