Skip to content

Commit

Permalink
增加多数据源的回滚测试
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Jul 7, 2024
1 parent 1424e3d commit b1b9146
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
8 changes: 4 additions & 4 deletions docs/dev-guide/orm/data-change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

所有实体都从OrmEntity继承,在OrmEntity上提供了修改状态跟踪机制

1. orm\_propDirty(propId) 可以判断某个字段是否已经被修改
2. orm\_propOldValue(propId) 返回修改前的值。如果没有被修改,则返回当前的值
3. orm\_propValue(propId) 返回字段当前的值
4. orm\_dirtyOldValues()和orm\_dirtyNewValues() 返回所有被修改的字段的修改前和修改后的值,返回的Map的key为属性名
1. `orm_propDirty(propId)` 可以判断某个字段是否已经被修改
2. `orm_propOldValue(propId)` 返回修改前的值。如果没有被修改,则返回当前的值
3. `orm_propValue(propId)` 返回字段当前的值
4. `orm_dirtyOldValues()``orm_dirtyNewValues()` 返回所有被修改的字段的修改前和修改后的值,返回的Map的key为属性名

## 修改监听器

Expand Down
9 changes: 5 additions & 4 deletions docs/dev-guide/orm/session-cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ NopORM与Hibernate类似,作为一个完整设计的ORM引擎,它提供了Se
```javascript
List<MyEntity> list = dao.findPage(query1);
assertEquals("abc", list.get(0).getName());

list.get(0).setName("sss");

List<MyEntity> list2 = dao.findPage(query2);
assertTrue(list.get(0) == list2.get(0));
assertEquals("sss", list2.get(0).getName());

assertEquals("sss", dao.getEntityById(list2.get(0).getId());
```
Expand Down Expand Up @@ -55,6 +55,7 @@ OrmEntity内置了一个Map类型的缓存属性`_t`,我们在报表引擎中
```javascript
entity.make_t().put("total",computeTotal());
entity.get_t().get("total");
entity.computeIfAbsent(key, loader);
```
## 概念层面不属于实体对象的缓存数据
Expand All @@ -66,7 +67,7 @@ interface IOrmTemplate{
ICache<Object, Object> sessionCache();

<T> T cacheGet(Object key, Supplier<T> loader);
}
}
```
通过NopORM引擎获取数据时,可以指定缓存的key对象。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ protected void doRollback(Throwable error) {
if (connection == null)
return;

LOG.info("nop.dao.jdbc.rollback:{}",this);
JdbcHelper.rollback(connection, dialect);
if (eagerReleaseConnection)
releaseConnection();
Expand Down
37 changes: 35 additions & 2 deletions nop-orm-geo/src/test/java/io/nop/orm/geo/TestMultiDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,33 @@
import io.nop.api.core.ioc.BeanContainerStartMode;
import io.nop.autotest.junit.JunitBaseTestCase;
import io.nop.core.lang.eval.IEvalScope;
import io.nop.dao.jdbc.IJdbcTemplate;
import io.nop.dao.txn.ITransactionTemplate;
import io.nop.orm.IOrmEntity;
import io.nop.orm.IOrmTemplate;
import io.nop.orm.sql_lib.ISqlLibManager;
import io.nop.xlang.api.XLang;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;

import jakarta.inject.Inject;
import java.util.HashMap;
import java.util.Map;

@NopTestConfig(localDb = true, initDatabaseSchema = true,beanContainerStartMode = BeanContainerStartMode.DEFAULT)
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

@NopTestConfig(localDb = true, initDatabaseSchema = true, beanContainerStartMode = BeanContainerStartMode.DEFAULT)
public class TestMultiDataSource extends JunitBaseTestCase {

@Inject
IOrmTemplate ormTemplate;

@Inject
IJdbcTemplate jdbcTemplate;

@Inject
ITransactionTemplate transactionTemplate;

@Inject
ISqlLibManager sqlLibManager;

Expand All @@ -35,4 +47,25 @@ public void testSqlQuerySpace() {
Map<String, Object> args = new HashMap<>();
sqlLibManager.invoke("test.getAllLocations", null, args, scope);
}

@Test
public void testTransaction() {
try {
ormTemplate.runInSession(() -> {
transactionTemplate.runInTransaction(txn -> {
IOrmEntity entity = ormTemplate.newEntity("test.TestGeo");
entity.orm_propValueByName("sid","123");
entity.orm_propValueByName("name", "test");
ormTemplate.save(entity);
ormTemplate.flushSession();

jdbcTemplate.existsTable(null,"test");
throw new IllegalStateException("test-error");
});
});
fail();
} catch (Exception e) {
assertTrue(e instanceof IllegalStateException);
}
}
}

0 comments on commit b1b9146

Please sign in to comment.