產生訂單號

來源:互聯網
上載者:User

標籤:hashcode   並發   uuid   


產生訂單號,最大的問題是要考慮高並發的時候.在網上找的一些方案往往在這種情況下難以適用,比如,查詢訂單序列最大數,根據系統時間產生等.

再考慮另一種可行的方案:資料庫遞增序列,沒錯,確實能解決高並發的問題,但在叢集等情況下對資料庫壓力比較大,而且有些資料庫對遞增序列沒有很好的支援,同時遞增序列的位元是一直增加的,也有可能會超出資料庫表欄位限制.


最好的方案是用java的uuid工具類產生唯一的字串,然後再取這個字串對象的hashcode值,因為字串的值是唯一的,它的hashcode也是唯一的.

可參考如下代碼:


 /** 產生訂單號,訂單號的組成: 兩位年+兩位月+兩位日+uuid雜湊號(由於hashcode傳回型別是int,最大為2147483648,不超過十位,所以此訂單號不超過16位)    * @return   * @throws Exception   **/public  String buildOrderSn() throws Exception {Date createDate = new  Date();SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd");StringBuilder out  = new StringBuilder(dateFormat.format(createDate));dateFormat = new SimpleDateFormat("yyyy-MM-dd");int uuidNum = UUID.randomUUID().toString().hashCode();out.append(uuidNum);return out.toString();}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.