{"id":2064,"date":"2016-06-10T14:42:20","date_gmt":"2016-06-10T06:42:20","guid":{"rendered":"http:\/\/boweihe.me\/?p=2064"},"modified":"2016-06-10T14:42:20","modified_gmt":"2016-06-10T06:42:20","slug":"hibernate-community-documentation-%e4%b9%8b-chapter-5-locking-%e9%94%81%ef%bc%88%e7%bf%bb%e8%af%91%ef%bc%89","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2064","title":{"rendered":"Hibernate Community Documentation \u4e4b Chapter 5. Locking \u9501\uff08\u7ffb\u8bd1\uff09"},"content":{"rendered":"<blockquote><p>\u79c1\u4eba\u7ffb\u8bd1\uff0c\u5982\u6709\u9519\u8bef\u8bf7\u5927\u80c6\u6307\u51fa\uff0c\u6211\u76f8\u4fe1\u4e0d\u4f1a\u6709\u4eba\u8f6c\u8f7d\uff1a\uff09<br \/>\n\u539f\u6587\u89c1<a href=\"https:\/\/docs.jboss.org\/hibernate\/orm\/4.0\/devguide\/en-US\/html\/ch05.html\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/docs.jboss.org\/hibernate\/orm\/4.0\/devguide\/en-US\/html\/ch05.html<\/a><\/p><\/blockquote>\n<p>\u6240\u8c13\u201c\u9501\u201d\u662f\u6307\u7528\u6765\u5728\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e2d\u9632\u6b62\u5728\u6570\u636e\u8bfb\u53d6(read)\u5230\u6570\u636e\u4f7f\u7528(used)\u4e4b\u95f4\u5176\u5185\u5bb9\u53d1\u751f\u53d8\u5316\u7684\u64cd\u4f5c\u3002<br \/>\n\u9501\u7684\u5b9e\u73b0\u5206\u4e3a\u4e24\u79cd\uff0c\u5206\u522b\u662f\u4e50\u89c2(Optimistic)\u4e0e\u60b2\u89c2(Pessimestic)\u3002<\/p>\n<h4>\u9501\u7b56\u7565<\/h4>\n<h5>\u4e50\u89c2\u9501<\/h5>\n<p>\u4e50\u89c2\u9501\u5047\u8bbe\u591a\u7ec4\u4e8b\u52a1(transaction)\u80fd\u591f\u4e92\u4e0d\u5e72\u6d89\u5730\u5e76\u53d1\u5b8c\u6210\uff0c\u56e0\u6b64\u8fd9\u4e9b\u4e8b\u52a1\u6267\u884c\u65f6\u4e0d\u9700\u8981\u5c06\u4ed6\u4eec\u5f71\u54cd\u5230\u7684\u6570\u636e\u52a0\u9501\u3002\u5728\u4e8b\u52a1\u63d0\u4ea4(commit)\u524d\uff0c\u6bcf\u4e2a\u4e8b\u52a1\u90fd\u4f1a\u6821\u9a8c\u4ee5\u786e\u4fdd\u5b83\u7684\u6570\u636e\u6ca1\u6709\u88ab\u522b\u4eba\u6539\u52a8\u8fc7\u3002\u5982\u679c\u6821\u9a8c\u7684\u7ed3\u679c\u662f\u5df2\u6709\u4fee\u6539\u51b2\u7a81(conflicting modifications)\uff0c\u6574\u4e2a\u4e8b\u52a1\u5c31\u4f1a\u56de\u6eda(rollback)\u3002<\/p>\n<h5>\u60b2\u89c2\u9501<\/h5>\n<p>\u60b2\u89c2\u9501\u5047\u8bbe\u4e8b\u52a1\u4e4b\u95f4\u4f1a\u76f8\u4e92\u51b2\u7a81\uff0c\u56e0\u6b64\u8981\u6c42\u5728\u6570\u636e\u8bfb\u53d6\u65f6\u4e0a\u9501\uff0c\u76f4\u81f3\u6570\u636e\u4f7f\u7528\u5b8c\u6bd5\u540e\u624d\u91ca\u653e\u9501\u3002<br \/>\n&nbsp;<br \/>\nHibernate\u4e3a\u4ee5\u4e0a\u4e24\u79cd\u9501\u7b56\u7565\u90fd\u63d0\u4f9b\u4e86\u652f\u6301\u3002<\/p>\n<h2>5.1 \u4e50\u89c2\u9501 Optimistic<\/h2>\n<p>\u5982\u679c\u4f60\u7684\u5e94\u7528\u4f7f\u7528\u4e86\u957f\u4e8b\u52a1\u6216\u662f\u7531\u8bb8\u591a\u6570\u636e\u5e93\u4e8b\u52a1\u7ec4\u6210\u7684\u4f1a\u8bdd(conversation)\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b58\u50a8\u7248\u672c(version)\u6570\u636e\u4f7f\u5f97\u5f53\u4e00\u4e2a\u5b9e\u4f53(entity)\u88ab\u4e24\u4e2a\u4f1a\u8bdd\u66f4\u65b0\u65f6\uff0c\u6700\u540e\u4e00\u4e2a\u63d0\u4ea4\u66f4\u6539\u7684\u4f1a\u8bdd\u4f1a\u88ab\u544a\u77e5\u51fa\u73b0\u51b2\u7a81\uff0c\u5e76\u4e14\u6b64\u65f6\u4e0d\u4f1a\u5f71\u54cd\u5230\u524d\u4e00\u4e2a\u4f1a\u8bdd\u8fdb\u884c\u7684\u66f4\u6539\u3002\u8be5\u65b9\u6cd5\u4fdd\u8bc1\u4e86\u4e00\u5b9a\u7684\u9694\u79bb\u6027\uff0c\u6269\u5c55\u6027\u597d\u4e14\u7279\u522b\u9002\u7528\u4e8e\u5e38\u8bfb\u53d6\u3001\u5c11\u5199\u5165(Read-Often Write-Sometimes)\u7684\u573a\u666f\u3002<\/p>\n<h3>5.1.1 \u4e13\u7528\u7248\u672c\u53f7 Dedicated version number<\/h3>\n<p>\u5b9e\u73b0\u4e50\u89c2\u9501\u7684\u7248\u672c\u53f7\u673a\u5236\u662f\u901a\u8fc7<span class=\"lang:default decode:true crayon-inline \">@Version<\/span>\u00a0\u6ce8\u91ca\u6765\u5b9e\u73b0\u7684<\/p>\n<pre class=\"lang:java decode:true \" title=\"\u4f8b5.1 @Version\u6ce8\u91ca\">@Entity\npublic class Flight implements Serializable {\n...\n    @Version\n    @Column(name=\"OPTLOCK\")\n    public Integer getVersion() { ... }\n}<\/pre>\n<p>\u6b64\u5904\uff0cVersion\u5c5e\u6027\u88ab\u6620\u5c04\u5230OPTLOCK\u5217\uff0c\u5b9e\u4f53\u7ba1\u7406\u5668(the entity manager)\u901a\u8fc7\u8fd9\u4e2a\u6ce8\u91ca\u6765\u68c0\u6d4b\u6709\u51b2\u7a81\u7684\u66f4\u65b0\uff0c\u501f\u6b64\u9632\u6b62\u6570\u636e\u66f4\u65b0\u88ab\u6700\u540e\u4e00\u4e2a\u5199\u5165\u63d0\u4ea4\u64cd\u4f5c\u7ed9\u8986\u76d6(last-commit-wins)\u3002<br \/>\nVersion\u5217\u53ef\u4ee5\u662f\u4efb\u610f\u7c7b\u578b\u7684\uff0c\u53ea\u8981\u4f60\u5b9a\u4e49\u5e76\u4e14\u5b9e\u73b0\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684UserVersionType\u5373\u53ef\u3002<br \/>\nHibernate\u7981\u6b62\u4f60\u7684\u5e94\u7528\u624b\u52a8\u53d8\u66f4\u7248\u672c\u53f7(version number)\u3002\u5982\u9700\u624b\u52a8\u589e\u52a0\u7248\u672c\u53f7\uff0c\u8bf7\u67e5\u770b\u4e0b\u5217\u76f8\u5173\u6587\u6863\uff1a<br \/>\n<span class=\"property\">LockModeType.OPTIMISTIC_FORCE_INCREMENT<\/span>\u00a0\u6216\u00a0<span class=\"property\">LockModeType.PESSIMISTIC_FORCE_INCREMENTcheck\u3002<\/span><\/p>\n<blockquote><p>\u5982\u679c\u7248\u672c\u53f7\u662f\u7531\u6570\u636e\u5e93\u901a\u8fc7\u6ce8\u5165triggert\u81ea\u52a8\u751f\u6210\u7684\uff0c\u9700\u4f7f\u7528\u6ce8\u91ca<\/p>\n<div class=\"note\">\n<span class=\"lang:default decode:true crayon-inline \">@org.hibernate.annotations.Generated(GenerationTime.ALWAYS)<\/span>\n<\/div>\n<\/blockquote>\n<pre class=\"lang:xhtml decode:true \" title=\"\u4f8b5.2 \u4f7f\u7528hbm.xml\u5b9a\u4e49\u7248\u672c\u5c5e\u6027\">&lt;version\n        column=\"version_column\"\n        name=\"propertyName\"\n        type=\"typename\"\n        access=\"field|property|ClassName\"\n        unsaved-value=\"null|negative|undefined\"\n        generated=\"never|always\"\n        insert=\"true|false\"\n        node=\"element-name|@attribute-name|element\/@attribute|.\"\n\/&gt;\n<\/pre>\n<div class=\"example-contents\">\n<div class=\"informaltable\">\n<table border=\"1\">\n<tbody>\n<tr>\n<td>column<\/td>\n<td>\u5b58\u50a8\u7248\u672c\u53f7\u7684\u5217\u540d. \u53ef\u9009\uff0c\u9ed8\u8ba4\u662f\u5c5e\u6027\u540d.<\/td>\n<\/tr>\n<tr>\n<td>name<\/td>\n<td>\u9700\u8981\u6301\u4e45\u5316\u7684\u7c7b\u540d<\/td>\n<\/tr>\n<tr>\n<td>type<\/td>\n<td>\u7248\u672c\u53f7\u5b58\u50a8\u7c7b\u578b. \u53ef\u9009\uff0c\u9ed8\u8ba4\u662f\u00a0<code class=\"literal\">integer<\/code>.<\/td>\n<\/tr>\n<tr>\n<td>access<\/td>\n<td>Hibernate\u8bbf\u95ee\u5c5e\u6027\u65f6\u4f7f\u7528\u7684\u65b9\u5f0f. \u53ef\u9009, \u9ed8\u8ba4\u662f\u00a0<code class=\"literal\">property<\/code>.<\/td>\n<\/tr>\n<tr>\n<td>unsaved-value<\/td>\n<td>Indicates that an instance is newly instantiated and thus unsaved. This distinguishes it from detached instances that were saved or loaded in a previous session. The default value,<code class=\"literal\">undefined<\/code>, indicates that the identifier property value should be used. Optional.<\/td>\n<\/tr>\n<tr>\n<td>generated<\/td>\n<td>\u8868\u660e\u8fd9\u662f\u7531\u6570\u636e\u5e93\u751f\u6210\u7684\u7248\u672c\u53f7. \u53ef\u9009\uff0c\u9ed8\u8ba4\u662f<code class=\"literal\">never<\/code>.<\/td>\n<\/tr>\n<tr>\n<td>insert<\/td>\n<td>\u662f\u5426\u5728SQL <code class=\"code\">insert<\/code>\u00a0\u8bed\u53e5\u4e2d\u52a0\u5165\u7248\u672c\u53f7. \u9ed8\u8ba4\u662f\u00a0<code class=\"literal\">true<\/code>, \u4f46\u5982\u679c\u4f60\u7684\u7248\u672c\u53f7\u5217\u6709\u9ed8\u8ba4\u503c0\u7684\u8bdd\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u6210false.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h3>5.1.2 \u65f6\u95f4\u6233 Timestamp<\/h3>\n<p>\u4e0e\u7248\u672c\u53f7\u76f8\u6bd4\uff0c\u65f6\u95f4\u6233\u5728\u5b9e\u73b0\u9501\u65f6\u7a0d\u663e\u4e0d\u53ef\u9760\u4e00\u4e9b\uff0c\u4f46\u5982\u679c\u5e94\u7528\u60f3\u8981\u501f\u6b64\u5b9e\u73b0\u4e00\u4e9b\u5176\u4ed6\u529f\u80fd\uff0c\u8fd8\u662f\u6709\u7528\u7684\u3002\u5982\u679c<span class=\"lang:default decode:true crayon-inline \">@Version<\/span>\u00a0\u6ce8\u91ca\u5e94\u7528\u5728Date\u6216Calendar\u5bf9\u8c61\u4e0a\u65f6\uff0c\u4f1a\u81ea\u52a8\u91c7\u7528\u65f6\u95f4\u6233\u5b9e\u73b0\u3002<\/p>\n<pre class=\"lang:java decode:true \" title=\"\u4f8b5.3 \u4f7f\u7528\u65f6\u95f4\u6233\u5b9e\u73b0\u4e50\u89c2\u9501\">@Entity\npublic class Flight implements Serializable {\n...\n    @Version\n    public Date getLastUpdate() { ... }\n}<\/pre>\n<p>\u5982\u679c\u4f60\u7528\u6ce8\u91ca<span class=\"lang:default decode:true crayon-inline \">@org.hibernate.annotations.Source<\/span>\u00a0\u7279\u522b\u6307\u5b9a\uff0cHibernate\u53ef\u4ee5\u4ece\u6570\u636e\u5e93\u6216\u8005JVM\u4e2d\u64b7\u53d6\u65f6\u95f4\u6233\u7684\u503c\uff1a\u53ef\u4ee5\u662forg.hibernate.annotations.SourceType.DB\u6216\u8005\u662forg.hibernate.annotations.SourceType.VM\uff0c\u5982\u679c\u4e0d\u7279\u522b\u6307\u5b9a\uff0c\u9ed8\u8ba4\u503c\u662f\u4f7f\u7528\u6570\u636e\u5e93\u7684\u65f6\u95f4\u6233\u3002<br \/>\n\u5982\u679c\u6307\u5b9a\u4e86<span class=\"lang:default decode:true crayon-inline \">@org.hibernate.annotations.Generated(GenerationTime.ALWAYS)<\/span>\u00a0\u6ce8\u91ca\uff0c\u65f6\u95f4\u6233\u7684\u503c\u53ef\u4ee5\u501f\u7531\u6570\u636e\u5e93\u76f4\u63a5\u751f\u6210\uff0c\u800c\u4e0d\u662fHibernate\u3002<\/p>\n<pre class=\"lang:xhtml decode:true \" title=\"\u4f8b5.4 \u5728hbm.xml\u4e2d\u58f0\u660e\u65f6\u95f4\u6233\">&lt;timestamp\n        column=\"timestamp_column\"\n        name=\"propertyName\"\n        access=\"field|property|ClassName\"\n        unsaved-value=\"null|undefined\"\n        source=\"vm|db\"\n        generated=\"never|always\"\n        node=\"element-name|@attribute-name|element\/@attribute|.\"\n\/&gt;<\/pre>\n<div class=\"example-contents\">\n<div class=\"informaltable\">\n<table border=\"1\">\n<colgroup>\n<col \/>\n<col \/><\/colgroup>\n<tbody>\n<tr>\n<td>column<\/td>\n<td>\u5b58\u653e\u65f6\u95f4\u6233\u7684\u5217\u540d\u3002\u53ef\u9009\uff0c\u9ed8\u8ba4\u503c\u4e3a\u5c5e\u6027\u540d\u3002<\/td>\n<\/tr>\n<tr>\n<td>name<\/td>\n<td>The name of a JavaBeans style property of Java type Date or Timestamp of the persistent class.<\/td>\n<\/tr>\n<tr>\n<td>access<\/td>\n<td>Hibernate\u7528\u4e8e\u8bbf\u95ee\u5c5e\u6027\u7684\u7b56\u7565\u3002\u53ef\u9009\uff0c\u9ed8\u8ba4\u4e3aproperty\u3002<\/td>\n<\/tr>\n<tr>\n<td>unsaved-value<\/td>\n<td>A version property which indicates than instance is newly instantiated, and unsaved. This distinguishes it from detached instances that were saved or loaded in a previous session. The default value of <code class=\"literal\">undefined<\/code> indicates that Hibernate uses the identifier property value.<\/td>\n<\/tr>\n<tr>\n<td>source<\/td>\n<td>Whether Hibernate retrieves the timestamp from the database or the current JVM. Database-based timestamps incur an overhead because Hibernate needs to query the database each time to determine the incremental next value. However, database-derived timestamps are safer to use in a clustered environment. Not all database dialects are known to support the retrieval of the database&#8217;s current timestamp. Others may also be unsafe for locking, because of lack of precision.<\/td>\n<\/tr>\n<tr>\n<td>generated<\/td>\n<td>\u662f\u5426\u7531\u6570\u636e\u5e93\u5185\u90e8\u81ea\u52a8\u751f\u6210\u65f6\u95f4\u6233\u7684\u503c\u3002\u53ef\u9009\uff0c\u9ed8\u8ba4\u503c\u662fnever\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>5.2 \u60b2\u89c2\u9501 Pessimistic<\/h2>\n<p>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ea\u9700\u8981\u6307\u660e\u4e00\u4e2aJDBC\u7684\u9694\u79bb\u7b49\u7ea7(lsolation level)\uff0c\u628a\u9501\u7684\u95ee\u9898\u4ea4\u7ed9\u6570\u636e\u5e93\u5904\u7406\u5c31\u884c\u4e86\u3002\u5982\u679c\u4f60\u786e\u5b9e\u9700\u8981\u83b7\u5f97\u72ec\u5360\u9501(exclusive locks)\u6216\u662f\u5728\u4e8b\u52a1\u5f00\u59cb\u65f6\u91cd\u65b0\u5f97\u5230(re-obtain)\u9501\uff0cHibernate\u4f1a\u63d0\u4f9b\u7ed9\u4f60\u9700\u8981\u7684\u65b9\u6cd5\u3002<\/p>\n<blockquote><p>Hibernate\u603b\u662f\u4f7f\u7528\u6570\u636e\u5e93\u7684\u9501\u673a\u5236\uff0c\u4ece\u4e0d\u5728\u5185\u5b58\u4e2d\u7ed9\u5bf9\u8c61\u4e0a\u9501<\/p><\/blockquote>\n<h3>5.2.1 LockMode\u7c7b<\/h3>\n<p>LockMode\u7c7b\u6307\u5b9a\u4e86Hibernate\u80fd\u53d6\u5f97\u7684\u5404\u79cd\u9501\u7ea7\u522b\u3002<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td>LockMode.WRITE<\/td>\n<td>\u5728Hibernate\u6267\u884cupdate\u6216insert\u884c\u65f6\u81ea\u52a8\u83b7\u53d6<\/td>\n<\/tr>\n<tr>\n<td>LockMode.UPGRADE<\/td>\n<td>\u5728\u7528\u6237\u663e\u5f0f\u5730\u4f7f\u7528\u00a0<code class=\"code\">SELECT ... FOR UPDATE<\/code>\u00a0\u8bed\u53e5\u65f6\uff08\u4ec5\u5f53\u6570\u636e\u5e93\u652f\u6301\u8be5\u8bed\u6cd5\uff09<\/td>\n<\/tr>\n<tr>\n<td>LockMode.UPGRADE_NOWAIT<\/td>\n<td>\u5728\u7528\u6237\u5bf9Oracle\u6570\u636e\u5e93\u663e\u5f0f\u5730\u4f7f\u7528<code class=\"code\">SELECT ... FOR UPDATE NOWAIT<\/code>\u00a0\u8bed\u53e5\u65f6.<\/td>\n<\/tr>\n<tr>\n<td>LockMode.READ<\/td>\n<td>\u5728Hibernate\u4ee5Repeatable Read\u6216\u662fSerializable\u9694\u79bb\u7ea7\u522b\u8bfb\u53d6\u65f6\u81ea\u52a8\u83b7\u53d6\uff0c\u4ea6\u53ef\u7531\u7528\u6237\u663e\u5f0f\u5730\u83b7\u53d6<\/td>\n<\/tr>\n<tr>\n<td>LockMode.NONE<\/td>\n<td>\u4e0d\u542f\u7528\u9501\u3002\u5728\u4e8b\u7269\u7ed3\u675f\u540e\uff0c\u6240\u6709\u7684\u5bf9\u8c61\u5c06\u4f1a\u5207\u6362\u5230\u8fd9\u4e00\u72b6\u6001\u3002\u901a\u8fc7\u8c03\u7528update()\u6216saveOrUpdate()\u65b9\u6cd5\u4e0e\u4f1a\u8bdd(session)\u5173\u8054\u7684\u5bf9\u8c61\u4ea6\u4ee5\u8fd9\u79cd\u9501\u6a21\u5f0f\u542f\u52a8\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<br \/>\n&nbsp;<br \/>\n\u9644\u6ce8\uff1a\u6709\u5173\u9501\u7684\u5176\u4ed6\u53c2\u8003\u6587\u7ae0<\/p>\n<ul>\n<li><a href=\"http:\/\/benjaminwhx.com\/2016\/03\/04\/%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B8%8B%E9%AB%98%E5%B9%B6%E5%8F%91%E7%9A%84%E5%A4%84%E7%90%86%E8%AF%A6%E8%A7%A3\/\" target=\"_blank\" rel=\"noopener noreferrer\">\u5927\u6570\u636e\u4e0b\u9ad8\u5e76\u53d1\u7684\u5904\u7406\u8be6\u89e3<\/a><\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Optimistic_locking\" target=\"_blank\" rel=\"noopener noreferrer\">Optimistic concurrency control<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u79c1\u4eba\u7ffb\u8bd1\uff0c\u5982\u6709\u9519\u8bef\u8bf7\u5927\u80c6\u6307\u51fa\uff0c\u6211\u76f8\u4fe1\u4e0d\u4f1a\u6709\u4eba\u8f6c\u8f7d\uff1a\uff09 \u539f\u6587\u89c1https:\/\/docs.jboss.org\/hibernate\/orm\/4.0\/devguide\/en-US\/html\/ch05.html \u6240\u8c13\u201c\u9501\u201d\u662f\u6307\u7528\u6765\u5728\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e2d\u9632\u6b62\u5728\u6570\u636e\u8bfb\u53d6(read)\u5230\u6570\u636e\u4f7f\u7528(used)\u4e4b\u95f4\u5176\u5185\u5bb9\u53d1\u751f\u53d8\u5316\u7684\u64cd\u4f5c\u3002 \u9501\u7684\u5b9e\u73b0\u5206\u4e3a\u4e24\u79cd\uff0c\u5206\u522b\u662f\u4e50\u89c2(Optimistic)\u4e0e\u60b2\u89c2(Pessimestic)\u3002 \u9501\u7b56\u7565 \u4e50\u89c2\u9501 \u4e50\u89c2\u9501\u5047\u8bbe\u591a\u7ec4\u4e8b\u52a1(transaction)\u80fd\u591f\u4e92\u4e0d\u5e72\u6d89\u5730\u5e76\u53d1\u5b8c\u6210\uff0c\u56e0\u6b64\u8fd9\u4e9b\u4e8b\u52a1\u6267\u884c\u65f6\u4e0d\u9700\u8981\u5c06\u4ed6\u4eec\u5f71\u54cd\u5230\u7684\u6570\u636e\u52a0\u9501\u3002\u5728\u4e8b\u52a1\u63d0\u4ea4(commit)\u524d\uff0c\u6bcf\u4e2a\u4e8b\u52a1\u90fd\u4f1a\u6821\u9a8c\u4ee5\u786e\u4fdd\u5b83\u7684\u6570\u636e\u6ca1\u6709\u88ab\u522b\u4eba\u6539\u52a8\u8fc7\u3002\u5982\u679c\u6821\u9a8c\u7684\u7ed3\u679c\u662f\u5df2\u6709\u4fee\u6539\u51b2\u7a81(conflicting modifications)\uff0c\u6574\u4e2a\u4e8b\u52a1\u5c31\u4f1a\u56de\u6eda(rollback)\u3002 \u60b2\u89c2\u9501 \u60b2\u89c2\u9501\u5047\u8bbe\u4e8b\u52a1\u4e4b\u95f4\u4f1a\u76f8\u4e92\u51b2\u7a81\uff0c\u56e0\u6b64\u8981\u6c42\u5728\u6570\u636e\u8bfb\u53d6\u65f6\u4e0a\u9501\uff0c\u76f4\u81f3\u6570\u636e\u4f7f\u7528\u5b8c\u6bd5\u540e\u624d\u91ca\u653e\u9501\u3002 &nbsp; Hibernate\u4e3a\u4ee5\u4e0a\u4e24\u79cd\u9501\u7b56\u7565\u90fd\u63d0\u4f9b\u4e86\u652f\u6301\u3002 5.1 \u4e50\u89c2\u9501 Optimistic \u5982\u679c\u4f60\u7684\u5e94\u7528\u4f7f\u7528\u4e86\u957f\u4e8b\u52a1\u6216\u662f\u7531\u8bb8\u591a\u6570\u636e\u5e93\u4e8b\u52a1\u7ec4\u6210\u7684\u4f1a\u8bdd(conversation)\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b58\u50a8\u7248\u672c(version)\u6570\u636e\u4f7f\u5f97\u5f53\u4e00\u4e2a\u5b9e\u4f53(entity)\u88ab\u4e24\u4e2a\u4f1a\u8bdd\u66f4\u65b0\u65f6\uff0c\u6700\u540e\u4e00\u4e2a\u63d0\u4ea4\u66f4\u6539\u7684\u4f1a\u8bdd\u4f1a\u88ab\u544a\u77e5\u51fa\u73b0\u51b2\u7a81\uff0c\u5e76\u4e14\u6b64\u65f6\u4e0d\u4f1a\u5f71\u54cd\u5230\u524d\u4e00\u4e2a\u4f1a\u8bdd\u8fdb\u884c\u7684\u66f4\u6539\u3002\u8be5\u65b9\u6cd5\u4fdd\u8bc1\u4e86\u4e00\u5b9a\u7684\u9694\u79bb\u6027\uff0c\u6269\u5c55\u6027\u597d\u4e14\u7279\u522b\u9002\u7528\u4e8e\u5e38\u8bfb\u53d6\u3001\u5c11\u5199\u5165(Read-Often Write-Sometimes)\u7684\u573a\u666f\u3002 5.1.1 \u4e13\u7528\u7248\u672c\u53f7 Dedicated version number \u5b9e\u73b0\u4e50\u89c2\u9501\u7684\u7248\u672c\u53f7\u673a\u5236\u662f\u901a\u8fc7@Version\u00a0\u6ce8\u91ca\u6765\u5b9e\u73b0\u7684 @Entity public class Flight implements Serializable { &#8230; @Version @Column(name=&#8221;OPTLOCK&#8221;) public Integer getVersion() { &#8230; } } \u6b64\u5904\uff0cVersion\u5c5e\u6027\u88ab\u6620\u5c04\u5230OPTLOCK\u5217\uff0c\u5b9e\u4f53\u7ba1\u7406\u5668(the entity manager)\u901a\u8fc7\u8fd9\u4e2a\u6ce8\u91ca\u6765\u68c0\u6d4b\u6709\u51b2\u7a81\u7684\u66f4\u65b0\uff0c\u501f\u6b64\u9632\u6b62\u6570\u636e\u66f4\u65b0\u88ab\u6700\u540e\u4e00\u4e2a\u5199\u5165\u63d0\u4ea4\u64cd\u4f5c\u7ed9\u8986\u76d6(last-commit-wins)\u3002 Version\u5217\u53ef\u4ee5\u662f\u4efb\u610f\u7c7b\u578b\u7684\uff0c\u53ea\u8981\u4f60\u5b9a\u4e49\u5e76\u4e14\u5b9e\u73b0\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684UserVersionType\u5373\u53ef\u3002 Hibernate\u7981\u6b62\u4f60\u7684\u5e94\u7528\u624b\u52a8\u53d8\u66f4\u7248\u672c\u53f7(version number)\u3002\u5982\u9700\u624b\u52a8\u589e\u52a0\u7248\u672c\u53f7\uff0c\u8bf7\u67e5\u770b\u4e0b\u5217\u76f8\u5173\u6587\u6863\uff1a LockModeType.OPTIMISTIC_FORCE_INCREMENT\u00a0\u6216\u00a0LockModeType.PESSIMISTIC_FORCE_INCREMENTcheck\u3002 \u5982\u679c\u7248\u672c\u53f7\u662f\u7531\u6570\u636e\u5e93\u901a\u8fc7\u6ce8\u5165triggert\u81ea\u52a8\u751f\u6210\u7684\uff0c\u9700\u4f7f\u7528\u6ce8\u91ca @org.hibernate.annotations.Generated(GenerationTime.ALWAYS) &lt;version column=&#8221;version_column&#8221; name=&#8221;propertyName&#8221; type=&#8221;typename&#8221; access=&#8221;field|property|ClassName&#8221; unsaved-value=&#8221;null|negative|undefined&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[50,163,208],"class_list":["post-2064","post","type-post","status-publish","format-standard","hentry","category-study","tag-hibernate","tag-163","tag-208"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2064","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2064"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2064\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}