本次发布主要增加了分布式Sega事务支持,适合多数据源
-
按照社区建议,修改了了springboot 的 yml配置方式
-
修改了@Jackson和@UpdateTime,本来是用来作为例子,但社区开发者提供了较好的完整实现
-
增加Sega支持
<dependency> <groupId>com.ibeetl</groupId> <artifactId>beetlsql</artifactId> <version>3.0.10-RELEASE</version> </dependency>public class UserEntity{ @Jackson Map<String,Address> addresses; @UpdateTime LocalDateTime updateTime; }
在多库多数据源的场景下,ORM工具有三个挑战,一个是如何根据各种策略分库分表,一个是如何方便的查询多库的数据,另外一个是多库操作的事务 BeetlSQL能很好的支持分库分表策略,以及提供了简单的Sega事务支持。至于多库查询,则可以交给第三方SQL查询引擎,BeetlSQL也支持多种SQL查询引擎,比如Druid,PrestoSQL
1public interface UserMapper extends SegaMapper<User> { 2 @SegaUpdateSql( 3 sql="update stock set count=count+1 where id=?", 4 rollback = "update stock set count=count-1 where id=?" 5 ) 6 void addStock(String id); 7}
SegaMapper 的内置增删改查都实现了回滚操作
1public interface SegaMapper<T> { 2 /** sega 改造的接口**/ 3 @AutoMapper(SegaInsertAMI.class) 4 void insert(T entity); 5 6 @AutoMapper(SegaUpdateByIdAMI.class) 7 int updateById(T entity); 8 9 @AutoMapper(SegaDeleteByIdAMI.class) 10 int deleteById(Object key); 11 12 13 /** 正常接口 **/ 14 @AutoMapper(SingleAMI.class) 15 T single(Object key); 16 17 @AutoMapper(UniqueAMI.class) 18 T unique(Object key); 19}
如下是测试代码 SimpleTest
1SegaContext segaContext = SegaContext.segaContextFactory.current(); 2UserMapper userMapper = sqlManager.getMapper(UserMapper.class); 3long count = sqlManager.allCount(User.class); 4try{ 5 User user = new User(); 6 user.setName("abc"); 7 userMapper.insert(user); 8 User user2 = new User(); 9 user2.setName("abc"); 10 userMapper.insert(user2); 11 throw new RuntimeException("模拟异常"); 12}catch(RuntimeException ex){ 13 segaContext.rollback(); 14} 15long afterCount = sqlManager.allCount(User.class); 16Assert.assertEquals(count,afterCount);
注意,如果在Spring中要使得Sega起作用,需要设置Tranaction为None