In parallel, use Redis to prevent repeated data insertions (the database is not set to table fields only)

Source: Internet
Author: 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");//Query          system.out.println (Thread.CurrentThread (). GetName () + "Clist.size:" +clist.size ());         if (Clist.size ()  == 0) {             if (Redisutil.setnx ("name_" + "AAAAA",  "AAAAA")  == 1) {// Returns 0 if the data exists, does not exist return 1                course course =  New course ();                 course.setcname ("AAAAA");                 try{               &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;TESTSERVICE.ADD1 (course);//Insert                  }catch  (exception e)  {                      Redisutil.del ("name_" + "AAAAA");//Insert Exception Delete                      throw e;                 }                 system.out.println (Thread.CurrentThread (). GetName () + "success");                 redisutil.expire ("name_" + "AAAAA",  3);// Set failure time 3 seconds             }else{                 system.out.println ( Thread.CurrentThread (). GetName () + "exists");             }        }else{             system.out.println (Thread.CurrentThread (). GetName () + "false");         }    }}

redisutil tools can be searched online

The following test code:

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 (            } catch);   (interruptedexception e)  {                 // TODO Auto-generated catch block         &nbsP;       e.printstacktrace ();             }            new  thread (new runnable ()  {                  @Override                  public void run ()  {                     // todo auto-generated  method stub                     post ();                 }            }). Start (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp;  }    }        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 ();         }    }}




In parallel, use Redis to prevent repeated data insertions (the database is not set to table fields only)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.