From 0589db8963e287d279fed96eb6b4dcf09d0a9cdb Mon Sep 17 00:00:00 2001 From: Junling Bu Date: Mon, 3 Sep 2018 15:56:25 +0800 Subject: [PATCH 01/11] =?UTF-8?q?chore[litemall-db]:=20=E5=86=99=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E4=B9=90=E8=A7=82=E9=94=81=E6=9B=B4=E6=96=B0=E7=9A=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../litemall/db/OptimisticLockTest.java | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 litemall-db/src/test/java/org/linlinjava/litemall/db/OptimisticLockTest.java diff --git a/litemall-db/src/test/java/org/linlinjava/litemall/db/OptimisticLockTest.java b/litemall-db/src/test/java/org/linlinjava/litemall/db/OptimisticLockTest.java new file mode 100644 index 00000000..05701222 --- /dev/null +++ b/litemall-db/src/test/java/org/linlinjava/litemall/db/OptimisticLockTest.java @@ -0,0 +1,120 @@ +package org.linlinjava.litemall.db; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.linlinjava.litemall.db.dao.LitemallSystemMapper; +import org.linlinjava.litemall.db.domain.LitemallSystem; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +/* + main线程先select,然后睡眠1s,最后做update + another线程则是先select,然后做update + + main: select update 2 + another: select update 3 + + 如果没有乐观锁,那么最后main线程的update操作是成功的,最终数据库保存的值是2; + 如果设置乐观锁,那么最后main线程的update操作是失败的,最终数据库保存的值是3。 + + 在一些业务过程中,需要采用乐观锁,这样可以保证数据更新时不会出现问题。 + */ +@WebAppConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest +public class OptimisticLockTest { + + @Autowired + private LitemallSystemMapper systemMapper; + + private Integer unlockId; + private Integer lockId; + + @Before + public void before() { + LitemallSystem unlockSystemConfig = new LitemallSystem(); + unlockSystemConfig.setKeyName("test-unlocksystem-key"); + unlockSystemConfig.setKeyValue("test-unlocksystem-value-1"); + systemMapper.insertSelective(unlockSystemConfig); + unlockId = unlockSystemConfig.getId(); + + LitemallSystem lockSystemConfig = new LitemallSystem(); + lockSystemConfig.setKeyName("test-locksystem-key"); + lockSystemConfig.setKeyValue("test-locksystem-value-1"); + systemMapper.insertSelective(lockSystemConfig); + lockId = lockSystemConfig.getId(); + } + + @After + public void after() { + systemMapper.deleteByPrimaryKey(unlockId); + unlockId = null; + systemMapper.deleteByPrimaryKey(lockId); + lockId = null; + } + + + @Test + public void runWithOptimisticLock() { + LitemallSystem mainSystem = systemMapper.selectByPrimaryKey(lockId); + + new Thread(new Runnable() { + @Override + public void run() { + LitemallSystem anotherSystem = systemMapper.selectByPrimaryKey(lockId); + + anotherSystem.setKeyValue("test-locksystem-value-3"); + systemMapper.updateWithVersionByPrimaryKey(anotherSystem.getVersion(), anotherSystem); + } + }).start(); + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + mainSystem.setKeyValue("test-locksystem-value-2"); + int updates = systemMapper.updateWithVersionByPrimaryKey(mainSystem.getVersion(), mainSystem); + Assert.assertEquals(updates, 0); + + mainSystem = systemMapper.selectByPrimaryKey(lockId); + Assert.assertNotEquals(mainSystem.getKeyValue(), "test-locksystem-value-2"); + Assert.assertEquals(mainSystem.getKeyValue(), "test-locksystem-value-3"); + } + + @Test + public void runWithoutOptimisticLock() { + LitemallSystem mainSystem = systemMapper.selectByPrimaryKey(unlockId); + + new Thread(new Runnable() { + @Override + public void run() { + LitemallSystem anotherSystem = systemMapper.selectByPrimaryKey(unlockId); + + anotherSystem.setKeyValue("test-unlocksystem-value-3"); + systemMapper.updateByPrimaryKey(anotherSystem); + } + }).start(); + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + mainSystem.setKeyValue("test-unlocksystem-value-2"); + int updates = systemMapper.updateByPrimaryKey(mainSystem); + Assert.assertEquals(updates, 1); + + mainSystem = systemMapper.selectByPrimaryKey(unlockId); + Assert.assertEquals(mainSystem.getKeyValue(), "test-unlocksystem-value-2"); + Assert.assertNotEquals(mainSystem.getKeyValue(), "test-unlocksystem-value-3"); + } +} + From ace17d24fc12afb8f621afacda1e9d28f17b7f65 Mon Sep 17 00:00:00 2001 From: Junling Bu Date: Mon, 3 Sep 2018 17:17:10 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E9=87=8D=E6=9E=84:=20=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=90=8E=E5=8F=B0=E4=B8=8D=E6=94=AF=E6=8C=81=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=91=98=E5=88=9B=E5=BB=BA=E5=92=8C=E7=BC=96=E8=BE=91=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E7=9A=84=E5=9C=B0=E5=9D=80=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/web/AdminAddressController.java | 49 ----- litemall-admin/src/api/address.js | 32 --- litemall-admin/src/views/user/address.vue | 205 +----------------- .../db/service/LitemallAddressService.java | 6 +- 4 files changed, 2 insertions(+), 290 deletions(-) diff --git a/litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminAddressController.java b/litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminAddressController.java index e74b8f20..6f25550d 100644 --- a/litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminAddressController.java +++ b/litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminAddressController.java @@ -78,53 +78,4 @@ public class AdminAddressController { return ResponseUtil.ok(data); } - - @PostMapping("/create") - public Object create(@LoginAdmin Integer adminId, @RequestBody LitemallAddress address){ - if(adminId == null){ - return ResponseUtil.unlogin(); - } - - String mobile = address.getMobile(); - if(!RegexUtil.isMobileExact(mobile)){ - return ResponseUtil.fail(403, "手机号格式不正确"); - } - - address.setAddTime(LocalDateTime.now()); - addressService.add(address); - - Map addressVo = toVo(address); - return ResponseUtil.ok(addressVo); - } - - @GetMapping("/read") - public Object read(@LoginAdmin Integer adminId, @NotNull Integer id){ - if(adminId == null){ - return ResponseUtil.unlogin(); - } - - LitemallAddress address = addressService.findById(id); - Map addressVo = toVo(address); - return ResponseUtil.ok(addressVo); - } - - @PostMapping("/update") - public Object update(@LoginAdmin Integer adminId, @RequestBody LitemallAddress address){ - if(adminId == null){ - return ResponseUtil.unlogin(); - } - addressService.updateById(address); - Map addressVo = toVo(address); - return ResponseUtil.ok(addressVo); - } - - @PostMapping("/delete") - public Object delete(@LoginAdmin Integer adminId, @RequestBody LitemallAddress address){ - if(adminId == null){ - return ResponseUtil.unlogin(); - } - addressService.delete(address.getId()); - return ResponseUtil.ok(); - } - } diff --git a/litemall-admin/src/api/address.js b/litemall-admin/src/api/address.js index 2056434e..6c899433 100644 --- a/litemall-admin/src/api/address.js +++ b/litemall-admin/src/api/address.js @@ -7,35 +7,3 @@ export function listAddress(query) { params: query }) } - -export function createAddress(data) { - return request({ - url: '/address/create', - method: 'post', - data - }) -} - -export function readAddress(data) { - return request({ - url: '/address/read', - method: 'get', - data - }) -} - -export function updateAddress(data) { - return request({ - url: '/address/update', - method: 'post', - data - }) -} - -export function deleteAddress(data) { - return request({ - url: '/address/delete', - method: 'post', - data - }) -} diff --git a/litemall-admin/src/views/user/address.vue b/litemall-admin/src/views/user/address.vue index eb871543..cd6bb63f 100644 --- a/litemall-admin/src/views/user/address.vue +++ b/litemall-admin/src/views/user/address.vue @@ -8,7 +8,6 @@ 查找 - 添加 导出 @@ -38,12 +37,6 @@ - - - @@ -53,57 +46,11 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -