@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)