並發下,使用redis防止資料重複插入(資料庫未對錶欄位設定唯一情況下)

來源:互聯網
上載者:User

標籤:

@Controller@RequestMapping("/myTest")public class TestController {    @Autowired(required = false)    private TestService testService;        @Autowired    private RedisUtil redisUtil;    @RequestMapping("add")    public void add(HttpServletRequest request) throws Exception {        List<Course> cList = testService.queryByName("aaaaa");//查詢        System.out.println(Thread.currentThread().getName()+"clist.size:"+cList.size());        if(cList.size() == 0){            if(redisUtil.setnx("name_"+"aaaaa", "aaaaa") == 1){//如果資料存在則返回0,不存在返回1                Course course = new Course();                course.setCname("aaaaa");                try{                    testService.add1(course);//插入                }catch (Exception e) {                    redisUtil.del("name_"+"aaaaa");//插入出異常則刪除                    throw e;                }                System.out.println(Thread.currentThread().getName()+"success");                redisUtil.expire("name_"+"aaaaa", 3);//設失效時間3秒            }else{                System.out.println(Thread.currentThread().getName()+"exists");            }        }else{            System.out.println(Thread.currentThread().getName()+"false");        }    }}

RedisUtil 工具類可以網上搜

以下測試代碼:

public class Test {    /**     * @param args     * @throws IOException      * @throws HttpException      */    public static void main(String[] args) throws Exception {        for(int i=0;i<10;i++){            try {                Thread.sleep(100);            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            new Thread(new Runnable() {                @Override                public void run() {                    // TODO Auto-generated method stub                    post();                }            }).start();        }    }        public static void post(){        HttpClient client = new HttpClient();        client.getParams().setContentCharset("UTF-8");        PostMethod method = new PostMethod("http://localhost:8080/practice/myTest/add.do?time="+new Date().getTime());        method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");        try {            client.executeMethod(method);        } catch (HttpException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}




並發下,使用redis防止資料重複插入(資料庫未對錶欄位設定唯一情況下)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.