[{"data":1,"prerenderedAt":2315},["ShallowReactive",2],{"page-en-\u002Fblog\u002Fspring-transactional-and-locking":3,"related-en-\u002Fblog\u002Fspring-transactional-and-locking":2294},{"id":4,"title":5,"body":6,"date":2280,"description":2281,"extension":2282,"meta":2283,"navigation":101,"path":2284,"seo":2285,"stem":2286,"tags":2287,"translation":2292,"__hash__":2293},"pages_en\u002Fen\u002Fblog\u002Fspring-transactional-and-locking.md","@Transactional Pitfalls and Locking with Spring and Hibernate",{"type":7,"value":8,"toc":2264},"minimark",[9,14,22,25,29,40,50,56,60,244,255,269,283,287,301,387,390,446,449,453,456,503,506,517,527,531,542,545,771,781,785,788,971,974,978,1096,1099,1102,1296,1299,1303,1309,1316,1334,1345,1349,1356,1442,1445,1503,1510,1517,1672,1684,1688,1691,1837,1872,1878,1882,1889,2082,2089,2093,2174,2186,2231,2240,2244,2260],[10,11,13],"h2",{"id":12},"introduction","Introduction",[15,16,17,21],"p",{},[18,19,20],"code",{},"@Transactional"," is Spring's most used and most misunderstood annotation. Putting it on a method usually works as expected; but in some cases no transaction is opened at all, an error doesn't trigger a rollback, or the connection pool runs dry. And with concurrent updates, picking the wrong locking strategy means data gets silently overwritten.",[15,23,24],{},"This post covers the most common pitfalls and the locking options with Hibernate, with examples.",[10,26,28],{"id":27},"_1-how-does-transactional-work","1. How does @Transactional work?",[15,30,31,32,34,35,39],{},"For a bean with ",[18,33,20],{},", Spring creates a ",[36,37,38],"strong",{},"proxy",". An external call goes to the proxy first; the proxy opens the transaction, calls the real method, then commits or rolls back:",[41,42,48],"pre",{"className":43,"code":45,"language":46,"meta":47},[44],"language-text","Controller ──► [Proxy: begin tx] ──► OrderService.placeOrder() ──► [Proxy: commit\u002Frollback]\n","text","",[18,49,45],{"__ignoreMap":47},[15,51,52,53],{},"The natural consequence of this model: ",[36,54,55],{},"calls that don't go through the proxy don't see the transaction rules.",[10,57,59],{"id":58},"_2-the-self-invocation-pitfall","2. The self-invocation pitfall",[41,61,65],{"className":62,"code":63,"language":64,"meta":47,"style":47},"language-java shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","@Service\npublic class OrderService {\n\n    public void placeOrders(List\u003COrderRequest> requests) {\n        for (var r : requests) {\n            placeOrder(r);              \u002F\u002F this.placeOrder() — bypasses the proxy!\n        }\n    }\n\n    @Transactional\n    public void placeOrder(OrderRequest request) { ... }\n}\n","java",[18,66,67,80,96,103,140,166,184,190,196,201,210,238],{"__ignoreMap":47},[68,69,72,76],"span",{"class":70,"line":71},"line",1,[68,73,75],{"class":74},"sMK4o","@",[68,77,79],{"class":78},"spNyl","Service\n",[68,81,83,86,89,93],{"class":70,"line":82},2,[68,84,85],{"class":78},"public",[68,87,88],{"class":78}," class",[68,90,92],{"class":91},"sBMFI"," OrderService",[68,94,95],{"class":74}," {\n",[68,97,99],{"class":70,"line":98},3,[68,100,102],{"emptyLinePlaceholder":101},true,"\n",[68,104,106,109,112,116,119,122,125,128,131,135,138],{"class":70,"line":105},4,[68,107,108],{"class":78},"    public",[68,110,111],{"class":78}," void",[68,113,115],{"class":114},"s2Zo4"," placeOrders",[68,117,118],{"class":74},"(",[68,120,121],{"class":78},"List",[68,123,124],{"class":74},"\u003C",[68,126,127],{"class":78},"OrderRequest",[68,129,130],{"class":74},">",[68,132,134],{"class":133},"sHdIc"," requests",[68,136,137],{"class":74},")",[68,139,95],{"class":74},[68,141,143,147,150,153,157,160,162,164],{"class":70,"line":142},5,[68,144,146],{"class":145},"s7zQu","        for",[68,148,149],{"class":74}," (",[68,151,152],{"class":78},"var",[68,154,156],{"class":155},"sTEyZ"," r ",[68,158,159],{"class":145},":",[68,161,134],{"class":155},[68,163,137],{"class":74},[68,165,95],{"class":74},[68,167,169,172,174,177,180],{"class":70,"line":168},6,[68,170,171],{"class":114},"            placeOrder",[68,173,118],{"class":74},[68,175,176],{"class":155},"r",[68,178,179],{"class":74},");",[68,181,183],{"class":182},"sHwdD","              \u002F\u002F this.placeOrder() — bypasses the proxy!\n",[68,185,187],{"class":70,"line":186},7,[68,188,189],{"class":74},"        }\n",[68,191,193],{"class":70,"line":192},8,[68,194,195],{"class":74},"    }\n",[68,197,199],{"class":70,"line":198},9,[68,200,102],{"emptyLinePlaceholder":101},[68,202,204,207],{"class":70,"line":203},10,[68,205,206],{"class":74},"    @",[68,208,209],{"class":78},"Transactional\n",[68,211,213,215,217,220,222,224,227,229,232,235],{"class":70,"line":212},11,[68,214,108],{"class":78},[68,216,111],{"class":78},[68,218,219],{"class":114}," placeOrder",[68,221,118],{"class":74},[68,223,127],{"class":78},[68,225,226],{"class":133}," request",[68,228,137],{"class":74},[68,230,231],{"class":74}," {",[68,233,234],{"class":74}," ...",[68,236,237],{"class":74}," }\n",[68,239,241],{"class":70,"line":240},12,[68,242,243],{"class":74},"}\n",[15,245,246,247,250,251,254],{},"Because ",[18,248,249],{},"placeOrders"," calls ",[18,252,253],{},"placeOrder"," from inside the same class, the annotation never kicks in. Fixes:",[256,257,258,262],"ul",{},[259,260,261],"li",{},"Move the transactional method to a separate bean (the cleanest way).",[259,263,264,265,268],{},"Use ",[18,266,267],{},"TransactionTemplate"," for programmatic transactions (see below).",[15,270,271,272,274,275,278,279,282],{},"Similarly, with the default proxy mechanism ",[18,273,20],{}," has no effect on ",[18,276,277],{},"private"," methods and on ",[18,280,281],{},"final"," methods\u002Fclasses. Put the annotation on public service methods.",[10,284,286],{"id":285},"_3-checked-exceptions-dont-roll-back-by-default","3. Checked exceptions don't roll back by default",[15,288,289,290,293,294,297,298,159],{},"By default Spring only rolls back on ",[18,291,292],{},"RuntimeException"," and ",[18,295,296],{},"Error",". If a checked exception is thrown, the transaction is ",[36,299,300],{},"committed",[41,302,304],{"className":62,"code":303,"language":64,"meta":47,"style":47},"@Transactional\npublic void importFile(Path file) throws IOException {\n    repository.save(new ImportJob(file));\n    Files.readAllLines(file);   \u002F\u002F if this throws IOException, the row is still committed\n}\n",[18,305,306,312,337,364,383],{"__ignoreMap":47},[68,307,308,310],{"class":70,"line":71},[68,309,75],{"class":74},[68,311,209],{"class":78},[68,313,314,316,318,321,323,326,329,331,334],{"class":70,"line":82},[68,315,85],{"class":78},[68,317,111],{"class":78},[68,319,320],{"class":114}," importFile",[68,322,118],{"class":74},[68,324,325],{"class":78},"Path",[68,327,328],{"class":155}," file",[68,330,137],{"class":74},[68,332,333],{"class":155}," throws IOException ",[68,335,336],{"class":74},"{\n",[68,338,339,342,345,348,350,353,356,358,361],{"class":70,"line":98},[68,340,341],{"class":155},"    repository",[68,343,344],{"class":74},".",[68,346,347],{"class":114},"save",[68,349,118],{"class":74},[68,351,352],{"class":145},"new",[68,354,355],{"class":114}," ImportJob",[68,357,118],{"class":74},[68,359,360],{"class":155},"file",[68,362,363],{"class":74},"));\n",[68,365,366,369,371,374,376,378,380],{"class":70,"line":105},[68,367,368],{"class":155},"    Files",[68,370,344],{"class":74},[68,372,373],{"class":114},"readAllLines",[68,375,118],{"class":74},[68,377,360],{"class":155},[68,379,179],{"class":74},[68,381,382],{"class":182},"   \u002F\u002F if this throws IOException, the row is still committed\n",[68,384,385],{"class":70,"line":142},[68,386,243],{"class":74},[15,388,389],{},"State the behavior explicitly:",[41,391,393],{"className":62,"code":392,"language":64,"meta":47,"style":47},"@Transactional(rollbackFor = Exception.class)\npublic void importFile(Path file) throws IOException { ... }\n",[18,394,395,421],{"__ignoreMap":47},[68,396,397,399,402,404,407,410,413,415,418],{"class":70,"line":71},[68,398,75],{"class":74},[68,400,401],{"class":78},"Transactional",[68,403,118],{"class":74},[68,405,406],{"class":91},"rollbackFor",[68,408,409],{"class":74}," =",[68,411,412],{"class":155}," Exception",[68,414,344],{"class":74},[68,416,417],{"class":155},"class",[68,419,420],{"class":74},")\n",[68,422,423,425,427,429,431,433,435,437,439,442,444],{"class":70,"line":82},[68,424,85],{"class":78},[68,426,111],{"class":78},[68,428,320],{"class":114},[68,430,118],{"class":74},[68,432,325],{"class":78},[68,434,328],{"class":155},[68,436,137],{"class":74},[68,438,333],{"class":155},[68,440,441],{"class":74},"{",[68,443,234],{"class":74},[68,445,237],{"class":74},[15,447,448],{},"And if you catch and swallow the exception inside the method, the proxy never sees the error and commits.",[10,450,452],{"id":451},"_4-readonly-true","4. readOnly = true",[15,454,455],{},"On methods that only read:",[41,457,459],{"className":62,"code":458,"language":64,"meta":47,"style":47},"@Transactional(readOnly = true)\npublic List\u003COrderView> findRecentOrders() { ... }\n",[18,460,461,477],{"__ignoreMap":47},[68,462,463,465,467,469,472,474],{"class":70,"line":71},[68,464,75],{"class":74},[68,466,401],{"class":78},[68,468,118],{"class":74},[68,470,471],{"class":91},"readOnly",[68,473,409],{"class":74},[68,475,476],{"class":74}," true)\n",[68,478,479,481,484,486,489,491,494,497,499,501],{"class":70,"line":82},[68,480,85],{"class":78},[68,482,483],{"class":78}," List",[68,485,124],{"class":74},[68,487,488],{"class":155},"OrderView",[68,490,130],{"class":74},[68,492,493],{"class":114}," findRecentOrders",[68,495,496],{"class":74},"()",[68,498,231],{"class":74},[68,500,234],{"class":74},[68,502,237],{"class":74},[15,504,505],{},"Benefits:",[256,507,508,511,514],{},[259,509,510],{},"Hibernate doesn't flush the session and doesn't need to keep entity snapshots for dirty checking, saving memory and CPU.",[259,512,513],{},"A read-only hint is passed to the JDBC driver; some databases optimize for it.",[259,515,516],{},"With a routing datasource it can be used as the signal to send queries to a read replica.",[15,518,519,520,523,524,526],{},"A common pattern: ",[18,521,522],{},"@Transactional(readOnly = true)"," on the service class, and ",[18,525,20],{}," on the methods that write.",[10,528,530],{"id":529},"_5-propagation-required-and-requires_new","5. Propagation: REQUIRED and REQUIRES_NEW",[15,532,533,534,537,538,541],{},"The default ",[18,535,536],{},"REQUIRED"," joins an existing transaction if there is one, otherwise opens a new one. ",[18,539,540],{},"REQUIRES_NEW"," suspends the current transaction and opens an independent new one.",[15,543,544],{},"The classic use case is an audit log that must persist even if the main operation fails:",[41,546,548],{"className":62,"code":547,"language":64,"meta":47,"style":47},"@Service\npublic class AuditService {\n\n    @Transactional(propagation = Propagation.REQUIRES_NEW)\n    public void log(String action, String detail) {\n        auditRepository.save(new AuditEntry(action, detail));\n    }\n}\n\n@Service\npublic class PaymentService {\n\n    @Transactional\n    public void pay(PaymentRequest request) {\n        auditService.log(\"PAYMENT_ATTEMPT\", request.id());   \u002F\u002F separate tx, commits right away\n        ...                                                   \u002F\u002F if this fails,\n    }                                                         \u002F\u002F the audit row stays\n}\n",[18,549,550,556,567,571,593,623,650,654,658,662,668,679,683,690,711,748,757,766],{"__ignoreMap":47},[68,551,552,554],{"class":70,"line":71},[68,553,75],{"class":74},[68,555,79],{"class":78},[68,557,558,560,562,565],{"class":70,"line":82},[68,559,85],{"class":78},[68,561,88],{"class":78},[68,563,564],{"class":91}," AuditService",[68,566,95],{"class":74},[68,568,569],{"class":70,"line":98},[68,570,102],{"emptyLinePlaceholder":101},[68,572,573,575,577,579,582,584,587,589,591],{"class":70,"line":105},[68,574,206],{"class":74},[68,576,401],{"class":78},[68,578,118],{"class":74},[68,580,581],{"class":91},"propagation",[68,583,409],{"class":74},[68,585,586],{"class":155}," Propagation",[68,588,344],{"class":74},[68,590,540],{"class":155},[68,592,420],{"class":74},[68,594,595,597,599,602,604,607,610,613,616,619,621],{"class":70,"line":142},[68,596,108],{"class":78},[68,598,111],{"class":78},[68,600,601],{"class":114}," log",[68,603,118],{"class":74},[68,605,606],{"class":78},"String",[68,608,609],{"class":133}," action",[68,611,612],{"class":74},",",[68,614,615],{"class":78}," String",[68,617,618],{"class":133}," detail",[68,620,137],{"class":74},[68,622,95],{"class":74},[68,624,625,628,630,632,634,636,639,641,644,646,648],{"class":70,"line":168},[68,626,627],{"class":155},"        auditRepository",[68,629,344],{"class":74},[68,631,347],{"class":114},[68,633,118],{"class":74},[68,635,352],{"class":145},[68,637,638],{"class":114}," AuditEntry",[68,640,118],{"class":74},[68,642,643],{"class":155},"action",[68,645,612],{"class":74},[68,647,618],{"class":155},[68,649,363],{"class":74},[68,651,652],{"class":70,"line":186},[68,653,195],{"class":74},[68,655,656],{"class":70,"line":192},[68,657,243],{"class":74},[68,659,660],{"class":70,"line":198},[68,661,102],{"emptyLinePlaceholder":101},[68,663,664,666],{"class":70,"line":203},[68,665,75],{"class":74},[68,667,79],{"class":78},[68,669,670,672,674,677],{"class":70,"line":212},[68,671,85],{"class":78},[68,673,88],{"class":78},[68,675,676],{"class":91}," PaymentService",[68,678,95],{"class":74},[68,680,681],{"class":70,"line":240},[68,682,102],{"emptyLinePlaceholder":101},[68,684,686,688],{"class":70,"line":685},13,[68,687,206],{"class":74},[68,689,209],{"class":78},[68,691,693,695,697,700,702,705,707,709],{"class":70,"line":692},14,[68,694,108],{"class":78},[68,696,111],{"class":78},[68,698,699],{"class":114}," pay",[68,701,118],{"class":74},[68,703,704],{"class":78},"PaymentRequest",[68,706,226],{"class":133},[68,708,137],{"class":74},[68,710,95],{"class":74},[68,712,714,717,719,722,724,727,731,733,735,737,739,742,745],{"class":70,"line":713},15,[68,715,716],{"class":155},"        auditService",[68,718,344],{"class":74},[68,720,721],{"class":114},"log",[68,723,118],{"class":74},[68,725,726],{"class":74},"\"",[68,728,730],{"class":729},"sfazB","PAYMENT_ATTEMPT",[68,732,726],{"class":74},[68,734,612],{"class":74},[68,736,226],{"class":155},[68,738,344],{"class":74},[68,740,741],{"class":114},"id",[68,743,744],{"class":74},"());",[68,746,747],{"class":182},"   \u002F\u002F separate tx, commits right away\n",[68,749,751,754],{"class":70,"line":750},16,[68,752,753],{"class":74},"        ...",[68,755,756],{"class":182},"                                                   \u002F\u002F if this fails,\n",[68,758,760,763],{"class":70,"line":759},17,[68,761,762],{"class":74},"    }",[68,764,765],{"class":182},"                                                         \u002F\u002F the audit row stays\n",[68,767,769],{"class":70,"line":768},18,[68,770,243],{"class":74},[15,772,773,774,777,778,780],{},"The cost: the inner transaction takes a ",[36,775,776],{},"second connection"," while the outer one still holds its own. With a pool size of 10 and 10 concurrent requests, each waiting for its second connection, the application can deadlock. Use ",[18,779,540],{}," carefully on hot paths and size the pool accordingly.",[10,782,784],{"id":783},"_6-programmatic-transactions-with-transactiontemplate","6. Programmatic transactions with TransactionTemplate",[15,786,787],{},"When you want to draw the transaction boundary clearly in code:",[41,789,791],{"className":62,"code":790,"language":64,"meta":47,"style":47},"@Service\npublic class ReportService {\n\n    private final TransactionTemplate tx;\n\n    ReportService(PlatformTransactionManager tm) {\n        this.tx = new TransactionTemplate(tm);\n    }\n\n    public Report generate(Long id) {\n        var data = tx.execute(status -> repository.loadReportData(id));\n        return renderPdf(data);   \u002F\u002F long-running work outside the transaction\n    }\n}\n",[18,792,793,799,810,814,831,835,852,876,880,884,906,945,963,967],{"__ignoreMap":47},[68,794,795,797],{"class":70,"line":71},[68,796,75],{"class":74},[68,798,79],{"class":78},[68,800,801,803,805,808],{"class":70,"line":82},[68,802,85],{"class":78},[68,804,88],{"class":78},[68,806,807],{"class":91}," ReportService",[68,809,95],{"class":74},[68,811,812],{"class":70,"line":98},[68,813,102],{"emptyLinePlaceholder":101},[68,815,816,819,822,825,828],{"class":70,"line":105},[68,817,818],{"class":78},"    private",[68,820,821],{"class":78}," final",[68,823,824],{"class":78}," TransactionTemplate",[68,826,827],{"class":155}," tx",[68,829,830],{"class":74},";\n",[68,832,833],{"class":70,"line":142},[68,834,102],{"emptyLinePlaceholder":101},[68,836,837,840,842,845,848,850],{"class":70,"line":168},[68,838,839],{"class":114},"    ReportService",[68,841,118],{"class":74},[68,843,844],{"class":78},"PlatformTransactionManager",[68,846,847],{"class":133}," tm",[68,849,137],{"class":74},[68,851,95],{"class":74},[68,853,854,857,860,863,866,868,870,873],{"class":70,"line":186},[68,855,856],{"class":74},"        this.",[68,858,859],{"class":155},"tx ",[68,861,862],{"class":74},"=",[68,864,865],{"class":145}," new",[68,867,824],{"class":114},[68,869,118],{"class":74},[68,871,872],{"class":155},"tm",[68,874,875],{"class":74},");\n",[68,877,878],{"class":70,"line":192},[68,879,195],{"class":74},[68,881,882],{"class":70,"line":198},[68,883,102],{"emptyLinePlaceholder":101},[68,885,886,888,891,894,896,899,902,904],{"class":70,"line":203},[68,887,108],{"class":78},[68,889,890],{"class":78}," Report",[68,892,893],{"class":114}," generate",[68,895,118],{"class":74},[68,897,898],{"class":78},"Long",[68,900,901],{"class":133}," id",[68,903,137],{"class":74},[68,905,95],{"class":74},[68,907,908,911,914,916,918,920,923,925,928,931,934,936,939,941,943],{"class":70,"line":212},[68,909,910],{"class":78},"        var",[68,912,913],{"class":155}," data ",[68,915,862],{"class":74},[68,917,827],{"class":155},[68,919,344],{"class":74},[68,921,922],{"class":114},"execute",[68,924,118],{"class":74},[68,926,927],{"class":155},"status ",[68,929,930],{"class":78},"->",[68,932,933],{"class":155}," repository",[68,935,344],{"class":74},[68,937,938],{"class":114},"loadReportData",[68,940,118],{"class":74},[68,942,741],{"class":155},[68,944,363],{"class":74},[68,946,947,950,953,955,958,960],{"class":70,"line":240},[68,948,949],{"class":145},"        return",[68,951,952],{"class":114}," renderPdf",[68,954,118],{"class":74},[68,956,957],{"class":155},"data",[68,959,179],{"class":74},[68,961,962],{"class":182},"   \u002F\u002F long-running work outside the transaction\n",[68,964,965],{"class":70,"line":685},[68,966,195],{"class":74},[68,968,969],{"class":70,"line":692},[68,970,243],{"class":74},[15,972,973],{},"There's no self-invocation problem either, since it doesn't rely on a proxy.",[10,975,977],{"id":976},"_7-keep-external-calls-out-of-transactions","7. Keep external calls out of transactions",[41,979,981],{"className":62,"code":980,"language":64,"meta":47,"style":47},"@Transactional\npublic void completeOrder(Long id) {\n    var order = orderRepository.findById(id).orElseThrow();\n    order.complete();\n    paymentClient.capture(order.paymentId());   \u002F\u002F may take 5 seconds\n    mailClient.sendReceipt(order);\n}\n",[18,982,983,989,1008,1039,1051,1076,1092],{"__ignoreMap":47},[68,984,985,987],{"class":70,"line":71},[68,986,75],{"class":74},[68,988,209],{"class":78},[68,990,991,993,995,998,1000,1002,1004,1006],{"class":70,"line":82},[68,992,85],{"class":78},[68,994,111],{"class":78},[68,996,997],{"class":114}," completeOrder",[68,999,118],{"class":74},[68,1001,898],{"class":78},[68,1003,901],{"class":155},[68,1005,137],{"class":74},[68,1007,95],{"class":74},[68,1009,1010,1013,1016,1018,1021,1023,1026,1028,1030,1033,1036],{"class":70,"line":98},[68,1011,1012],{"class":78},"    var",[68,1014,1015],{"class":155}," order ",[68,1017,862],{"class":74},[68,1019,1020],{"class":155}," orderRepository",[68,1022,344],{"class":74},[68,1024,1025],{"class":114},"findById",[68,1027,118],{"class":74},[68,1029,741],{"class":155},[68,1031,1032],{"class":74},").",[68,1034,1035],{"class":114},"orElseThrow",[68,1037,1038],{"class":74},"();\n",[68,1040,1041,1044,1046,1049],{"class":70,"line":105},[68,1042,1043],{"class":155},"    order",[68,1045,344],{"class":74},[68,1047,1048],{"class":114},"complete",[68,1050,1038],{"class":74},[68,1052,1053,1056,1058,1061,1063,1066,1068,1071,1073],{"class":70,"line":142},[68,1054,1055],{"class":155},"    paymentClient",[68,1057,344],{"class":74},[68,1059,1060],{"class":114},"capture",[68,1062,118],{"class":74},[68,1064,1065],{"class":155},"order",[68,1067,344],{"class":74},[68,1069,1070],{"class":114},"paymentId",[68,1072,744],{"class":74},[68,1074,1075],{"class":182},"   \u002F\u002F may take 5 seconds\n",[68,1077,1078,1081,1083,1086,1088,1090],{"class":70,"line":168},[68,1079,1080],{"class":155},"    mailClient",[68,1082,344],{"class":74},[68,1084,1085],{"class":114},"sendReceipt",[68,1087,118],{"class":74},[68,1089,1065],{"class":155},[68,1091,875],{"class":74},[68,1093,1094],{"class":70,"line":186},[68,1095,243],{"class":74},[15,1097,1098],{},"This code holds a database connection and row locks for the duration of the HTTP calls. And if the commit fails after the mail is sent, the customer gets a receipt for an order that never happened.",[15,1100,1101],{},"Move side effects to after the commit:",[41,1103,1105],{"className":62,"code":1104,"language":64,"meta":47,"style":47},"@Transactional\npublic void completeOrder(Long id) {\n    var order = orderRepository.findById(id).orElseThrow();\n    order.complete();\n    events.publishEvent(new OrderCompleted(order.getId()));\n}\n\n@Component\nclass OrderNotifications {\n\n    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)\n    void onCompleted(OrderCompleted event) {\n        mailClient.sendReceipt(event.orderId());\n    }\n}\n",[18,1106,1107,1113,1131,1155,1165,1194,1198,1202,1209,1218,1222,1246,1266,1288,1292],{"__ignoreMap":47},[68,1108,1109,1111],{"class":70,"line":71},[68,1110,75],{"class":74},[68,1112,209],{"class":78},[68,1114,1115,1117,1119,1121,1123,1125,1127,1129],{"class":70,"line":82},[68,1116,85],{"class":78},[68,1118,111],{"class":78},[68,1120,997],{"class":114},[68,1122,118],{"class":74},[68,1124,898],{"class":78},[68,1126,901],{"class":155},[68,1128,137],{"class":74},[68,1130,95],{"class":74},[68,1132,1133,1135,1137,1139,1141,1143,1145,1147,1149,1151,1153],{"class":70,"line":98},[68,1134,1012],{"class":78},[68,1136,1015],{"class":155},[68,1138,862],{"class":74},[68,1140,1020],{"class":155},[68,1142,344],{"class":74},[68,1144,1025],{"class":114},[68,1146,118],{"class":74},[68,1148,741],{"class":155},[68,1150,1032],{"class":74},[68,1152,1035],{"class":114},[68,1154,1038],{"class":74},[68,1156,1157,1159,1161,1163],{"class":70,"line":105},[68,1158,1043],{"class":155},[68,1160,344],{"class":74},[68,1162,1048],{"class":114},[68,1164,1038],{"class":74},[68,1166,1167,1170,1172,1175,1177,1179,1182,1184,1186,1188,1191],{"class":70,"line":142},[68,1168,1169],{"class":155},"    events",[68,1171,344],{"class":74},[68,1173,1174],{"class":114},"publishEvent",[68,1176,118],{"class":74},[68,1178,352],{"class":145},[68,1180,1181],{"class":114}," OrderCompleted",[68,1183,118],{"class":74},[68,1185,1065],{"class":155},[68,1187,344],{"class":74},[68,1189,1190],{"class":114},"getId",[68,1192,1193],{"class":74},"()));\n",[68,1195,1196],{"class":70,"line":168},[68,1197,243],{"class":74},[68,1199,1200],{"class":70,"line":186},[68,1201,102],{"emptyLinePlaceholder":101},[68,1203,1204,1206],{"class":70,"line":192},[68,1205,75],{"class":74},[68,1207,1208],{"class":78},"Component\n",[68,1210,1211,1213,1216],{"class":70,"line":198},[68,1212,417],{"class":78},[68,1214,1215],{"class":91}," OrderNotifications",[68,1217,95],{"class":74},[68,1219,1220],{"class":70,"line":203},[68,1221,102],{"emptyLinePlaceholder":101},[68,1223,1224,1226,1229,1231,1234,1236,1239,1241,1244],{"class":70,"line":212},[68,1225,206],{"class":74},[68,1227,1228],{"class":78},"TransactionalEventListener",[68,1230,118],{"class":74},[68,1232,1233],{"class":91},"phase",[68,1235,409],{"class":74},[68,1237,1238],{"class":155}," TransactionPhase",[68,1240,344],{"class":74},[68,1242,1243],{"class":155},"AFTER_COMMIT",[68,1245,420],{"class":74},[68,1247,1248,1251,1254,1256,1259,1262,1264],{"class":70,"line":240},[68,1249,1250],{"class":78},"    void",[68,1252,1253],{"class":114}," onCompleted",[68,1255,118],{"class":74},[68,1257,1258],{"class":78},"OrderCompleted",[68,1260,1261],{"class":133}," event",[68,1263,137],{"class":74},[68,1265,95],{"class":74},[68,1267,1268,1271,1273,1275,1277,1280,1282,1285],{"class":70,"line":685},[68,1269,1270],{"class":155},"        mailClient",[68,1272,344],{"class":74},[68,1274,1085],{"class":114},[68,1276,118],{"class":74},[68,1278,1279],{"class":155},"event",[68,1281,344],{"class":74},[68,1283,1284],{"class":114},"orderId",[68,1286,1287],{"class":74},"());\n",[68,1289,1290],{"class":70,"line":692},[68,1291,195],{"class":74},[68,1293,1294],{"class":70,"line":713},[68,1295,243],{"class":74},[15,1297,1298],{},"The listener only runs if the transaction commits successfully. If you need guaranteed delivery (the application can crash between the commit and the listener), prefer the outbox pattern.",[10,1300,1302],{"id":1301},"_8-lazyinitializationexception-and-open-in-view","8. LazyInitializationException and open-in-view",[41,1304,1307],{"className":1305,"code":1306,"language":46,"meta":47},[44],"org.hibernate.LazyInitializationException: could not initialize proxy - no Session\n",[18,1308,1306],{"__ignoreMap":47},[15,1310,1311,1312,1315],{},"It happens when a lazy association is accessed after the transaction (and session) is closed. Spring Boot's default ",[18,1313,1314],{},"spring.jpa.open-in-view=true"," hides this error by keeping the session open up to the view layer, but you pay for it with uncontrolled queries running in controllers and during JSON serialization.",[41,1317,1321],{"className":1318,"code":1319,"language":1320,"meta":47,"style":47},"language-properties shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","spring.jpa.open-in-view=false\n","properties",[18,1322,1323],{"__ignoreMap":47},[68,1324,1325,1329,1331],{"class":70,"line":71},[68,1326,1328],{"class":1327},"swJcz","spring.jpa.open-in-view",[68,1330,862],{"class":74},[68,1332,1333],{"class":155},"false\n",[15,1335,1336,1337,1340,1341,1344],{},"The right fix is to load the data you need inside the transaction with ",[18,1338,1339],{},"JOIN FETCH",", ",[18,1342,1343],{},"@EntityGraph"," or a DTO projection, and to return DTOs instead of entities from the service layer.",[10,1346,1348],{"id":1347},"_9-optimistic-locking-version","9. Optimistic locking: @Version",[15,1350,1351,1352,1355],{},"When two users update the same record at the same time, the later write silently overwrites the earlier one (lost update). ",[18,1353,1354],{},"@Version"," prevents that:",[41,1357,1359],{"className":62,"code":1358,"language":64,"meta":47,"style":47},"@Entity\npublic class Product {\n    @Id @GeneratedValue\n    private Long id;\n    private int stock;\n\n    @Version\n    private long version;\n}\n",[18,1360,1361,1368,1379,1392,1403,1415,1419,1426,1438],{"__ignoreMap":47},[68,1362,1363,1365],{"class":70,"line":71},[68,1364,75],{"class":74},[68,1366,1367],{"class":78},"Entity\n",[68,1369,1370,1372,1374,1377],{"class":70,"line":82},[68,1371,85],{"class":78},[68,1373,88],{"class":78},[68,1375,1376],{"class":91}," Product",[68,1378,95],{"class":74},[68,1380,1381,1383,1386,1389],{"class":70,"line":98},[68,1382,206],{"class":74},[68,1384,1385],{"class":78},"Id",[68,1387,1388],{"class":74}," @",[68,1390,1391],{"class":78},"GeneratedValue\n",[68,1393,1394,1396,1399,1401],{"class":70,"line":105},[68,1395,818],{"class":78},[68,1397,1398],{"class":78}," Long",[68,1400,901],{"class":155},[68,1402,830],{"class":74},[68,1404,1405,1407,1410,1413],{"class":70,"line":142},[68,1406,818],{"class":78},[68,1408,1409],{"class":78}," int",[68,1411,1412],{"class":155}," stock",[68,1414,830],{"class":74},[68,1416,1417],{"class":70,"line":168},[68,1418,102],{"emptyLinePlaceholder":101},[68,1420,1421,1423],{"class":70,"line":186},[68,1422,206],{"class":74},[68,1424,1425],{"class":78},"Version\n",[68,1427,1428,1430,1433,1436],{"class":70,"line":192},[68,1429,818],{"class":78},[68,1431,1432],{"class":78}," long",[68,1434,1435],{"class":155}," version",[68,1437,830],{"class":74},[68,1439,1440],{"class":70,"line":198},[68,1441,243],{"class":74},[15,1443,1444],{},"Hibernate adds a version check to every update:",[41,1446,1450],{"className":1447,"code":1448,"language":1449,"meta":47,"style":47},"language-sql shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","update product set stock = ?, version = 6 where id = ? and version = 5;\n","sql",[18,1451,1452],{"__ignoreMap":47},[68,1453,1454,1458,1461,1464,1467,1469,1472,1475,1477,1480,1483,1486,1488,1491,1494,1496,1498,1501],{"class":70,"line":71},[68,1455,1457],{"class":1456},"sbssI","update",[68,1459,1460],{"class":155}," product ",[68,1462,1463],{"class":1456},"set",[68,1465,1466],{"class":155}," stock ",[68,1468,862],{"class":74},[68,1470,1471],{"class":155}," ?, ",[68,1473,1474],{"class":1456},"version",[68,1476,409],{"class":74},[68,1478,1479],{"class":1456}," 6",[68,1481,1482],{"class":1456}," where",[68,1484,1485],{"class":155}," id ",[68,1487,862],{"class":74},[68,1489,1490],{"class":155}," ? ",[68,1492,1493],{"class":1456},"and",[68,1495,1435],{"class":1456},[68,1497,409],{"class":74},[68,1499,1500],{"class":1456}," 5",[68,1502,830],{"class":155},[15,1504,1505,1506,1509],{},"If the affected row count is 0, Spring throws ",[18,1507,1508],{},"ObjectOptimisticLockingFailureException",". When conflicts are rare, it gives you consistency without holding locks.",[15,1511,1512,1513,1516],{},"To retry on conflict, you can use ",[18,1514,1515],{},"@Retryable",", which came with Spring Framework 7:",[41,1518,1520],{"className":62,"code":1519,"language":64,"meta":47,"style":47},"@Configuration\n@EnableResilientMethods\nclass ResilienceConfig { }\n\n@Service\npublic class StockFacade {\n\n    private final StockService stockService;\n\n    @Retryable(includes = ObjectOptimisticLockingFailureException.class)\n    public void decrease(Long productId, int qty) {\n        stockService.decrease(productId, qty);   \u002F\u002F @Transactional method\n    }\n}\n",[18,1521,1522,1529,1536,1547,1551,1557,1568,1572,1586,1590,1613,1640,1664,1668],{"__ignoreMap":47},[68,1523,1524,1526],{"class":70,"line":71},[68,1525,75],{"class":74},[68,1527,1528],{"class":78},"Configuration\n",[68,1530,1531,1533],{"class":70,"line":82},[68,1532,75],{"class":74},[68,1534,1535],{"class":78},"EnableResilientMethods\n",[68,1537,1538,1540,1543,1545],{"class":70,"line":98},[68,1539,417],{"class":78},[68,1541,1542],{"class":91}," ResilienceConfig",[68,1544,231],{"class":74},[68,1546,237],{"class":74},[68,1548,1549],{"class":70,"line":105},[68,1550,102],{"emptyLinePlaceholder":101},[68,1552,1553,1555],{"class":70,"line":142},[68,1554,75],{"class":74},[68,1556,79],{"class":78},[68,1558,1559,1561,1563,1566],{"class":70,"line":168},[68,1560,85],{"class":78},[68,1562,88],{"class":78},[68,1564,1565],{"class":91}," StockFacade",[68,1567,95],{"class":74},[68,1569,1570],{"class":70,"line":186},[68,1571,102],{"emptyLinePlaceholder":101},[68,1573,1574,1576,1578,1581,1584],{"class":70,"line":192},[68,1575,818],{"class":78},[68,1577,821],{"class":78},[68,1579,1580],{"class":78}," StockService",[68,1582,1583],{"class":155}," stockService",[68,1585,830],{"class":74},[68,1587,1588],{"class":70,"line":198},[68,1589,102],{"emptyLinePlaceholder":101},[68,1591,1592,1594,1597,1599,1602,1604,1607,1609,1611],{"class":70,"line":203},[68,1593,206],{"class":74},[68,1595,1596],{"class":78},"Retryable",[68,1598,118],{"class":74},[68,1600,1601],{"class":91},"includes",[68,1603,409],{"class":74},[68,1605,1606],{"class":155}," ObjectOptimisticLockingFailureException",[68,1608,344],{"class":74},[68,1610,417],{"class":155},[68,1612,420],{"class":74},[68,1614,1615,1617,1619,1622,1624,1626,1629,1631,1633,1636,1638],{"class":70,"line":212},[68,1616,108],{"class":78},[68,1618,111],{"class":78},[68,1620,1621],{"class":114}," decrease",[68,1623,118],{"class":74},[68,1625,898],{"class":78},[68,1627,1628],{"class":133}," productId",[68,1630,612],{"class":74},[68,1632,1409],{"class":78},[68,1634,1635],{"class":133}," qty",[68,1637,137],{"class":74},[68,1639,95],{"class":74},[68,1641,1642,1645,1647,1650,1652,1655,1657,1659,1661],{"class":70,"line":240},[68,1643,1644],{"class":155},"        stockService",[68,1646,344],{"class":74},[68,1648,1649],{"class":114},"decrease",[68,1651,118],{"class":74},[68,1653,1654],{"class":155},"productId",[68,1656,612],{"class":74},[68,1658,1635],{"class":155},[68,1660,179],{"class":74},[68,1662,1663],{"class":182},"   \u002F\u002F @Transactional method\n",[68,1665,1666],{"class":70,"line":685},[68,1667,195],{"class":74},[68,1669,1670],{"class":70,"line":692},[68,1671,243],{"class":74},[15,1673,1674,1675,1678,1679,293,1681,1683],{},"The key point: ",[36,1676,1677],{},"the retry must wrap the transaction, not run inside it."," The optimistic lock failure happens at commit\u002Fflush time, and that transaction is now rollback-only. Each attempt must run in a new transaction, re-reading the current version. So put ",[18,1680,1515],{},[18,1682,20],{}," on separate beans, not on the same method.",[10,1685,1687],{"id":1686},"_10-pessimistic-locking","10. Pessimistic locking",[15,1689,1690],{},"When conflicts are frequent or retrying is expensive, lock the row while reading it:",[41,1692,1694],{"className":62,"code":1693,"language":64,"meta":47,"style":47},"public interface AccountRepository extends JpaRepository\u003CAccount, Long> {\n\n    @Lock(LockModeType.PESSIMISTIC_WRITE)\n    @QueryHints(@QueryHint(name = \"jakarta.persistence.lock.timeout\", value = \"3000\"))\n    @Query(\"select a from Account a where a.id = :id\")\n    Optional\u003CAccount> findForUpdate(Long id);\n}\n",[18,1695,1696,1725,1729,1748,1793,1811,1833],{"__ignoreMap":47},[68,1697,1698,1700,1703,1706,1709,1712,1714,1717,1719,1721,1723],{"class":70,"line":71},[68,1699,85],{"class":78},[68,1701,1702],{"class":78}," interface",[68,1704,1705],{"class":91}," AccountRepository",[68,1707,1708],{"class":78}," extends",[68,1710,1711],{"class":91}," JpaRepository",[68,1713,124],{"class":74},[68,1715,1716],{"class":78},"Account",[68,1718,612],{"class":74},[68,1720,1398],{"class":78},[68,1722,130],{"class":74},[68,1724,95],{"class":74},[68,1726,1727],{"class":70,"line":82},[68,1728,102],{"emptyLinePlaceholder":101},[68,1730,1731,1733,1736,1738,1741,1743,1746],{"class":70,"line":98},[68,1732,206],{"class":74},[68,1734,1735],{"class":78},"Lock",[68,1737,118],{"class":74},[68,1739,1740],{"class":155},"LockModeType",[68,1742,344],{"class":74},[68,1744,1745],{"class":155},"PESSIMISTIC_WRITE",[68,1747,420],{"class":74},[68,1749,1750,1752,1755,1758,1761,1763,1766,1768,1771,1774,1776,1778,1781,1783,1785,1788,1790],{"class":70,"line":105},[68,1751,206],{"class":74},[68,1753,1754],{"class":78},"QueryHints",[68,1756,1757],{"class":74},"(@",[68,1759,1760],{"class":78},"QueryHint",[68,1762,118],{"class":74},[68,1764,1765],{"class":91},"name",[68,1767,409],{"class":74},[68,1769,1770],{"class":74}," \"",[68,1772,1773],{"class":729},"jakarta.persistence.lock.timeout",[68,1775,726],{"class":74},[68,1777,612],{"class":74},[68,1779,1780],{"class":91}," value",[68,1782,409],{"class":74},[68,1784,1770],{"class":74},[68,1786,1787],{"class":729},"3000",[68,1789,726],{"class":74},[68,1791,1792],{"class":74},"))\n",[68,1794,1795,1797,1800,1802,1804,1807,1809],{"class":70,"line":142},[68,1796,206],{"class":74},[68,1798,1799],{"class":78},"Query",[68,1801,118],{"class":74},[68,1803,726],{"class":74},[68,1805,1806],{"class":729},"select a from Account a where a.id = :id",[68,1808,726],{"class":74},[68,1810,420],{"class":74},[68,1812,1813,1816,1818,1820,1822,1825,1827,1829,1831],{"class":70,"line":168},[68,1814,1815],{"class":78},"    Optional",[68,1817,124],{"class":74},[68,1819,1716],{"class":78},[68,1821,130],{"class":74},[68,1823,1824],{"class":114}," findForUpdate",[68,1826,118],{"class":74},[68,1828,898],{"class":78},[68,1830,901],{"class":133},[68,1832,875],{"class":74},[68,1834,1835],{"class":70,"line":186},[68,1836,243],{"class":74},[41,1838,1840],{"className":1447,"code":1839,"language":1449,"meta":47,"style":47},"select ... from account a where a.id = ? for update\n",[18,1841,1842],{"__ignoreMap":47},[68,1843,1844,1847,1850,1853,1856,1859,1862,1864,1866,1869],{"class":70,"line":71},[68,1845,1846],{"class":1456},"select",[68,1848,1849],{"class":155}," ... ",[68,1851,1852],{"class":1456},"from",[68,1854,1855],{"class":155}," account a ",[68,1857,1858],{"class":1456},"where",[68,1860,1861],{"class":155}," a.id ",[68,1863,862],{"class":74},[68,1865,1490],{"class":155},[68,1867,1868],{"class":1456},"for",[68,1870,1871],{"class":1456}," update\n",[15,1873,1874,1875,1877],{},"The lock is held until the end of the transaction, so the method must be ",[18,1876,20],{}," and short. How the lock timeout hint is applied depends on the database and dialect; verify the behavior in the SQL log. When locking multiple rows, always lock them in the same order (e.g. by id) to avoid deadlocks.",[10,1879,1881],{"id":1880},"_11-a-job-queue-with-skip-locked","11. A job queue with SKIP LOCKED",[15,1883,1884,1885,1888],{},"When multiple instances pull work from the same table, PostgreSQL's ",[18,1886,1887],{},"SKIP LOCKED"," skips rows locked by another worker, so workers don't wait on each other and never pick up the same job twice:",[41,1890,1892],{"className":62,"code":1891,"language":64,"meta":47,"style":47},"public interface JobRepository extends JpaRepository\u003CJob, Long> {\n\n    @Query(value = \"\"\"\n            select * from job\n            where status = 'PENDING'\n            order by created_at\n            limit :limit\n            for update skip locked\n            \"\"\", nativeQuery = true)\n    List\u003CJob> lockNextBatch(int limit);\n}\n\n@Transactional\npublic void processBatch() {\n    for (Job job : jobRepository.lockNextBatch(10)) {\n        job.markRunning();\n    }\n}\n",[18,1893,1894,1920,1924,1940,1945,1950,1955,1960,1965,1979,2003,2007,2011,2017,2030,2062,2074,2078],{"__ignoreMap":47},[68,1895,1896,1898,1900,1903,1905,1907,1909,1912,1914,1916,1918],{"class":70,"line":71},[68,1897,85],{"class":78},[68,1899,1702],{"class":78},[68,1901,1902],{"class":91}," JobRepository",[68,1904,1708],{"class":78},[68,1906,1711],{"class":91},[68,1908,124],{"class":74},[68,1910,1911],{"class":78},"Job",[68,1913,612],{"class":74},[68,1915,1398],{"class":78},[68,1917,130],{"class":74},[68,1919,95],{"class":74},[68,1921,1922],{"class":70,"line":82},[68,1923,102],{"emptyLinePlaceholder":101},[68,1925,1926,1928,1930,1932,1935,1937],{"class":70,"line":98},[68,1927,206],{"class":74},[68,1929,1799],{"class":78},[68,1931,118],{"class":74},[68,1933,1934],{"class":91},"value",[68,1936,409],{"class":74},[68,1938,1939],{"class":74}," \"\"\"\n",[68,1941,1942],{"class":70,"line":105},[68,1943,1944],{"class":729},"            select * from job\n",[68,1946,1947],{"class":70,"line":142},[68,1948,1949],{"class":729},"            where status = 'PENDING'\n",[68,1951,1952],{"class":70,"line":168},[68,1953,1954],{"class":729},"            order by created_at\n",[68,1956,1957],{"class":70,"line":186},[68,1958,1959],{"class":729},"            limit :limit\n",[68,1961,1962],{"class":70,"line":192},[68,1963,1964],{"class":729},"            for update skip locked\n",[68,1966,1967,1970,1972,1975,1977],{"class":70,"line":198},[68,1968,1969],{"class":74},"            \"\"\"",[68,1971,612],{"class":74},[68,1973,1974],{"class":91}," nativeQuery",[68,1976,409],{"class":74},[68,1978,476],{"class":74},[68,1980,1981,1984,1986,1988,1990,1993,1995,1998,2001],{"class":70,"line":203},[68,1982,1983],{"class":78},"    List",[68,1985,124],{"class":74},[68,1987,1911],{"class":78},[68,1989,130],{"class":74},[68,1991,1992],{"class":114}," lockNextBatch",[68,1994,118],{"class":74},[68,1996,1997],{"class":78},"int",[68,1999,2000],{"class":133}," limit",[68,2002,875],{"class":74},[68,2004,2005],{"class":70,"line":212},[68,2006,243],{"class":74},[68,2008,2009],{"class":70,"line":240},[68,2010,102],{"emptyLinePlaceholder":101},[68,2012,2013,2015],{"class":70,"line":685},[68,2014,75],{"class":74},[68,2016,209],{"class":78},[68,2018,2019,2021,2023,2026,2028],{"class":70,"line":692},[68,2020,85],{"class":78},[68,2022,111],{"class":78},[68,2024,2025],{"class":114}," processBatch",[68,2027,496],{"class":74},[68,2029,95],{"class":74},[68,2031,2032,2035,2037,2039,2042,2044,2047,2049,2052,2054,2057,2060],{"class":70,"line":713},[68,2033,2034],{"class":145},"    for",[68,2036,149],{"class":74},[68,2038,1911],{"class":78},[68,2040,2041],{"class":155}," job ",[68,2043,159],{"class":145},[68,2045,2046],{"class":155}," jobRepository",[68,2048,344],{"class":74},[68,2050,2051],{"class":114},"lockNextBatch",[68,2053,118],{"class":74},[68,2055,2056],{"class":1456},"10",[68,2058,2059],{"class":74},"))",[68,2061,95],{"class":74},[68,2063,2064,2067,2069,2072],{"class":70,"line":750},[68,2065,2066],{"class":155},"        job",[68,2068,344],{"class":74},[68,2070,2071],{"class":114},"markRunning",[68,2073,1038],{"class":74},[68,2075,2076],{"class":70,"line":759},[68,2077,195],{"class":74},[68,2079,2080],{"class":70,"line":768},[68,2081,243],{"class":74},[15,2083,2084,2085,2088],{},"For long-running jobs, the best approach is to mark the row as ",[18,2086,2087],{},"RUNNING"," like this and close the transaction, do the work outside the transaction, and write the result in a separate transaction.",[10,2090,2092],{"id":2091},"_12-isolation-levels","12. Isolation levels",[2094,2095,2096,2115],"table",{},[2097,2098,2099],"thead",{},[2100,2101,2102,2106,2109,2112],"tr",{},[2103,2104,2105],"th",{},"Level",[2103,2107,2108],{},"Dirty read",[2103,2110,2111],{},"Non-repeatable read",[2103,2113,2114],{},"Phantom read",[2116,2117,2118,2133,2147,2161],"tbody",{},[2100,2119,2120,2126,2129,2131],{},[2121,2122,2123],"td",{},[18,2124,2125],{},"READ_UNCOMMITTED",[2121,2127,2128],{},"Possible",[2121,2130,2128],{},[2121,2132,2128],{},[2100,2134,2135,2140,2143,2145],{},[2121,2136,2137],{},[18,2138,2139],{},"READ_COMMITTED",[2121,2141,2142],{},"No",[2121,2144,2128],{},[2121,2146,2128],{},[2100,2148,2149,2154,2156,2158],{},[2121,2150,2151],{},[18,2152,2153],{},"REPEATABLE_READ",[2121,2155,2142],{},[2121,2157,2142],{},[2121,2159,2160],{},"Possible*",[2100,2162,2163,2168,2170,2172],{},[2121,2164,2165],{},[18,2166,2167],{},"SERIALIZABLE",[2121,2169,2142],{},[2121,2171,2142],{},[2121,2173,2142],{},[15,2175,2176,2177,2179,2180,2182,2183,2185],{},"* PostgreSQL's ",[18,2178,2153],{}," implementation doesn't allow phantom reads either. The default is ",[18,2181,2139],{}," on PostgreSQL and Oracle, and ",[18,2184,2153],{}," on MySQL InnoDB.",[41,2187,2189],{"className":62,"code":2188,"language":64,"meta":47,"style":47},"@Transactional(isolation = Isolation.SERIALIZABLE)\npublic void transfer(...) { ... }\n",[18,2190,2191,2213],{"__ignoreMap":47},[68,2192,2193,2195,2197,2199,2202,2204,2207,2209,2211],{"class":70,"line":71},[68,2194,75],{"class":74},[68,2196,401],{"class":78},[68,2198,118],{"class":74},[68,2200,2201],{"class":91},"isolation",[68,2203,409],{"class":74},[68,2205,2206],{"class":155}," Isolation",[68,2208,344],{"class":74},[68,2210,2167],{"class":155},[68,2212,420],{"class":74},[68,2214,2215,2217,2219,2222,2225,2227,2229],{"class":70,"line":82},[68,2216,85],{"class":78},[68,2218,111],{"class":78},[68,2220,2221],{"class":114}," transfer",[68,2223,2224],{"class":74},"(...)",[68,2226,231],{"class":74},[68,2228,234],{"class":74},[68,2230,237],{"class":74},[15,2232,2233,2234,2236,2237,2239],{},"Higher isolation levels can cause serialization failures, which again require retrying the operation. For most applications ",[18,2235,2139],{}," + ",[18,2238,1354],{}," is the right balance.",[10,2241,2243],{"id":2242},"conclusion","Conclusion",[15,2245,2246,2247,2249,2250,2253,2254,2256,2257,2259],{},"Remembering that ",[18,2248,20],{}," is a proxy explains most of the pitfalls: self-invocation, private methods and swallowed exceptions. Keep transactions short, move external calls and side effects to after the commit, and turn off ",[18,2251,2252],{},"open-in-view",". For concurrency, the default choice should be optimistic locking with ",[18,2255,1354],{}," and a retry that wraps the transaction; use pessimistic locks for frequently contended records and ",[18,2258,1887],{}," for job queues.",[2261,2262,2263],"style",{},"html pre.shiki code .sMK4o, html code.shiki .sMK4o{--shiki-light:#39ADB5;--shiki-default:#89DDFF;--shiki-dark:#89DDFF}html pre.shiki code .spNyl, html code.shiki .spNyl{--shiki-light:#9C3EDA;--shiki-default:#C792EA;--shiki-dark:#C792EA}html pre.shiki code .sBMFI, html code.shiki .sBMFI{--shiki-light:#E2931D;--shiki-default:#FFCB6B;--shiki-dark:#FFCB6B}html pre.shiki code .s2Zo4, html code.shiki .s2Zo4{--shiki-light:#6182B8;--shiki-default:#82AAFF;--shiki-dark:#82AAFF}html pre.shiki code .sHdIc, html code.shiki .sHdIc{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#EEFFFF;--shiki-default-font-style:italic;--shiki-dark:#BABED8;--shiki-dark-font-style:italic}html pre.shiki code .s7zQu, html code.shiki .s7zQu{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#89DDFF;--shiki-default-font-style:italic;--shiki-dark:#89DDFF;--shiki-dark-font-style:italic}html pre.shiki code .sTEyZ, html code.shiki .sTEyZ{--shiki-light:#90A4AE;--shiki-default:#EEFFFF;--shiki-dark:#BABED8}html pre.shiki code .sHwdD, html code.shiki .sHwdD{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#546E7A;--shiki-default-font-style:italic;--shiki-dark:#676E95;--shiki-dark-font-style:italic}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sfazB, html code.shiki .sfazB{--shiki-light:#91B859;--shiki-default:#C3E88D;--shiki-dark:#C3E88D}html pre.shiki code .swJcz, html code.shiki .swJcz{--shiki-light:#E53935;--shiki-default:#F07178;--shiki-dark:#F07178}html pre.shiki code .sbssI, html code.shiki .sbssI{--shiki-light:#F76D47;--shiki-default:#F78C6C;--shiki-dark:#F78C6C}",{"title":47,"searchDepth":82,"depth":82,"links":2265},[2266,2267,2268,2269,2270,2271,2272,2273,2274,2275,2276,2277,2278,2279],{"id":12,"depth":82,"text":13},{"id":27,"depth":82,"text":28},{"id":58,"depth":82,"text":59},{"id":285,"depth":82,"text":286},{"id":451,"depth":82,"text":452},{"id":529,"depth":82,"text":530},{"id":783,"depth":82,"text":784},{"id":976,"depth":82,"text":977},{"id":1301,"depth":82,"text":1302},{"id":1347,"depth":82,"text":1348},{"id":1686,"depth":82,"text":1687},{"id":1880,"depth":82,"text":1881},{"id":2091,"depth":82,"text":2092},{"id":2242,"depth":82,"text":2243},"2026-11-22","How @Transactional works through proxies, self-invocation, rollback rules, propagation, readOnly, optimistic and pessimistic locking, and the SKIP LOCKED job-queue pattern.","md",{},"\u002Fen\u002Fblog\u002Fspring-transactional-and-locking",{"title":5,"description":2281},"en\u002Fblog\u002Fspring-transactional-and-locking",[64,2288,2289,2290,2291],"spring","hibernate","database","guide","\u002Fblog\u002Fspring-transactional-and-locking","_feucehsY-_6eXqUdYnlwFyfK3ApWqTwrmOeJTkGb9s",[2295,2302,2308],{"path":2296,"title":2297,"description":2298,"date":2299,"tags":2300},"\u002Fen\u002Fblog\u002Fhibernate-n-plus-one-problem","Solving the N+1 Query Problem in Hibernate and Spring Data JPA","How to detect the N+1 query problem and fix it with JOIN FETCH, @EntityGraph, @BatchSize, SUBSELECT and DTO projections — a practical guide with examples.","2026-11-21",[64,2289,2301,2290,2291],"jpa",{"path":2303,"title":2304,"description":2305,"date":2306,"tags":2307},"\u002Fen\u002Fblog\u002Fjpa-entity-mapping-best-practices","JPA and Hibernate Entity Mapping Best Practices","What to watch for when designing entities with Hibernate 7 and Spring Boot — ID strategies and batching, LAZY associations, bidirectional mappings, equals\u002FhashCode, @Version, auditing and schema management.","2026-11-20",[64,2289,2301,2290,2291],{"path":2309,"title":2310,"description":2311,"date":2312,"tags":2313},"\u002Fen\u002Fblog\u002Fspring-boot-virtual-threads","Virtual Threads in Spring Boot with Java 25","Enabling virtual threads in Spring Boot, how Java 24+ fixed the pinning problem, the connection-pool bottleneck, scoped values, structured concurrency and observability tips.","2026-11-19",[64,2288,2314,2291],"concurrency",1795309807423]