本次发布主要增加了分布式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
public interface UserMapper extends SegaMapper<User> { @SegaUpdateSql( sql="update stock set count=count+1 where id=?", rollback = "update stock set count=count-1 where id=?" ) void addStock(String id); }
SegaMapper 的内置增删改查都实现了回滚操作
public interface SegaMapper<T> { /** sega 改造的接口**/ @AutoMapper(SegaInsertAMI.class) void insert(T entity); @AutoMapper(SegaUpdateByIdAMI.class) int updateById(T entity); @AutoMapper(SegaDeleteByIdAMI.class) int deleteById(Object key); /** 正常接口 **/ @AutoMapper(SingleAMI.class) T single(Object key); @AutoMapper(UniqueAMI.class) T unique(Object key); }
如下是测试代码 SimpleTest
SegaContext segaContext = SegaContext.segaContextFactory.current(); UserMapper userMapper = sqlManager.getMapper(UserMapper.class); long count = sqlManager.allCount(User.class); try{ User user = new User(); user.setName("abc"); userMapper.insert(user); User user2 = new User(); user2.setName("abc"); userMapper.insert(user2); throw new RuntimeException("模拟异常"); }catch(RuntimeException ex){ segaContext.rollback(); } long afterCount = sqlManager.allCount(User.class); Assert.assertEquals(count,afterCount);
注意,如果在Spring中要使得Sega起作用,需要设置Tranaction为None
评论删除后,数据将无法恢复
BeetlSQL 3.0.10 发布,内置 sega 事务支持
本次发布主要增加了分布式Sega事务支持,适合多数据源
在多库多数据源的场景下,ORM工具有三个挑战,一个是如何根据各种策略分库分表,一个是如何方便的查询多库的数据,另外一个是多库操作的事务 BeetlSQL能很好的支持分库分表策略,以及提供了简单的Sega事务支持。至于多库查询,则可以交给第三方SQL查询引擎,BeetlSQL也支持多种SQL查询引擎,比如Druid,PrestoSQL
SegaMapper 的内置增删改查都实现了回滚操作
如下是测试代码 SimpleTest
注意,如果在Spring中要使得Sega起作用,需要设置Tranaction为None