1. 程式人生 > >ShardingJDBC不支持批量插入的一種解決辦法

ShardingJDBC不支持批量插入的一種解決辦法

gpo ins app war source ava tran user cep

最近項目中需要進行分表就使用了ShardingJDBC做數據庫中間層操作
之前sql中有寫批量插入,而ShardingJDBC不支持insert xxx values(...),(...),(...)語句
故需要手動開啟事務,循環插入並提交事務(與之前批量插入語句性能未做比較)

@Component
public class UserManager implements ApplicationContextAware {

    private static ApplicationContext applicationContext;
    @Autowired
    private UserDao UserDao;

    public
void createUseres(List<User> UserList) { DefaultTransactionDefinition definition = new DefaultTransactionDefinition(); definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); definition.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED
); DataSourceTransactionManager transactionManager = (DataSourceTransactionManager) applicationContext.getBean("transactionManager"); TransactionStatus transactionStatus = transactionManager.getTransaction(definition); for (User User : UserList) { //UserDao有create方法,單條插入
applicationContext.getBean(UserDao.class).create(User); } transactionManager.commit(transactionStatus); } @Override public void setApplicationContext(ApplicationContext ac) throws BeansException { applicationContext = ac; } }

ShardingJDBC不支持批量插入的一種解決辦法