[{"data":1,"prerenderedAt":327},["ShallowReactive",2],{"post:transaction-detection-hibernate":3},{"id":4,"title":5,"body":6,"date":313,"description":64,"draft":314,"extension":315,"meta":316,"navigation":317,"path":318,"seo":319,"stem":320,"tags":321,"__hash__":326},"posts\u002Fposts\u002Ftransaction-detection-hibernate.md","Cross-engine transaction detection in Hibernate v3+",{"type":7,"value":8,"toc":310},"minimark",[9,14,31,38,41,52,55,58,117,120,171,186,283,286,296,303,306],[10,11,13],"h3",{"id":12},"update-now-with-adobe-support","UPDATE: Now with Adobe support!",[15,16,17,18,22,23,26,27],"p",{},"So, this is embarrassing. This article ",[19,20,21],"em",{},"claimed"," to offer a one-liner with Adobe and Lucee compatibility, but I completely neglected to actually ",[19,24,25],{},"test"," it on ACF before I wrote the blog post. (I thought I'd tested it... evidently not.) I realized this a week after attempting to use my \"cross-compatible\" transaction detection code that ",[28,29,30],"strong",{},"it's not cross-compatible at all!",[15,32,33,34,37],{},"Bottom line: I updated the article with a properly-tested, fully cross-engine-compatible snippet to detect transactional context ",[28,35,36],{},"on both Adobe CF and Lucee",". You're welcome.",[39,40],"hr",{},[15,42,43,44,47,48,51],{},"We do a lot of ORM work at Ortus, and recently I have been on a bit of a Hibernate binge. One of the important things to note when working with Hibernate is that ORM queries may behave differently ",[19,45,46],{},"inside"," a transaction block than they do ",[19,49,50],{},"outside"," a transaction block. More importantly, the transactional handling in, say, Lucee 5.3 begins to break down when working with nested transactions.",[15,53,54],{},"I digress, but let's just say it's important to know when the current code is inside a transaction context.",[15,56,57],{},"Previously, the best knowledge we had said to run this on Adobe:",[59,60,65],"pre",{"className":61,"code":62,"language":63,"meta":64,"style":64},"language-js shiki shiki-themes github-light github-dark","    \u002F\u002F ADOBE ONLY!\n    var isInTransaction = createObject(\"java\", \"coldfusion.tagext.sql.TransactionTag\").getCurrent()\n","js","",[66,67,68,77],"code",{"__ignoreMap":64},[69,70,73],"span",{"class":71,"line":72},"line",1,[69,74,76],{"class":75},"sJ8bj","    \u002F\u002F ADOBE ONLY!\n",[69,78,80,84,88,91,95,98,102,105,108,111,114],{"class":71,"line":79},2,[69,81,83],{"class":82},"szBVR","    var",[69,85,87],{"class":86},"sVt8B"," isInTransaction ",[69,89,90],{"class":82},"=",[69,92,94],{"class":93},"sScJk"," createObject",[69,96,97],{"class":86},"(",[69,99,101],{"class":100},"sZZnC","\"java\"",[69,103,104],{"class":86},", ",[69,106,107],{"class":100},"\"coldfusion.tagext.sql.TransactionTag\"",[69,109,110],{"class":86},").",[69,112,113],{"class":93},"getCurrent",[69,115,116],{"class":86},"()\n",[15,118,119],{},"and something like this on Lucee:",[59,121,123],{"className":61,"code":122,"language":63,"meta":64,"style":64},"    \u002F\u002F LUCEE ONLY!\n    var isInTransaction = getPageContext().getDataSourceManager().isAutoCommit() ? false : true;\n",[66,124,125,130],{"__ignoreMap":64},[69,126,127],{"class":71,"line":72},[69,128,129],{"class":75},"    \u002F\u002F LUCEE ONLY!\n",[69,131,132,134,136,138,141,144,147,149,152,155,158,162,165,168],{"class":71,"line":79},[69,133,83],{"class":82},[69,135,87],{"class":86},[69,137,90],{"class":82},[69,139,140],{"class":93}," getPageContext",[69,142,143],{"class":86},"().",[69,145,146],{"class":93},"getDataSourceManager",[69,148,143],{"class":86},[69,150,151],{"class":93},"isAutoCommit",[69,153,154],{"class":86},"() ",[69,156,157],{"class":82},"?",[69,159,161],{"class":160},"sj4cs"," false",[69,163,164],{"class":82}," :",[69,166,167],{"class":160}," true",[69,169,170],{"class":86},";\n",[15,172,173,174,181,182,185],{},"But digging through the ",[175,176,180],"a",{"href":177,"rel":178},"https:\u002F\u002Fdocs.jboss.org\u002Fhibernate\u002Fstable\u002Fcore\u002Fjavadocs\u002Forg\u002Fhibernate\u002Fengine\u002Fspi\u002FSharedSessionContractImplementor.html#isTransactionInProgress--",[179],"nofollow","Hibernate Session docs",", I found a wonderful little method called ",[66,183,184],{},"isTransactionInProgress",". And just like that, here's a CFML method to wrap it and consistently detect the transactional context:",[59,187,189],{"className":61,"code":188,"language":63,"meta":64,"style":64},"boolean function isInTransaction(){\n    if ( listFindNoCase( \"Lucee\", server.coldfusion.productname ) ) {\n        return ORMGetSession().isTransactionInProgress();\n    } else {\n        return ORMGetSession().getActualSession().isTransactionInProgress();\n    }\n}\n",[66,190,191,205,225,241,253,271,277],{"__ignoreMap":64},[69,192,193,196,199,202],{"class":71,"line":72},[69,194,195],{"class":86},"boolean ",[69,197,198],{"class":82},"function",[69,200,201],{"class":93}," isInTransaction",[69,203,204],{"class":86},"(){\n",[69,206,207,210,213,216,219,222],{"class":71,"line":79},[69,208,209],{"class":82},"    if",[69,211,212],{"class":86}," ( ",[69,214,215],{"class":93},"listFindNoCase",[69,217,218],{"class":86},"( ",[69,220,221],{"class":100},"\"Lucee\"",[69,223,224],{"class":86},", server.coldfusion.productname ) ) {\n",[69,226,228,231,234,236,238],{"class":71,"line":227},3,[69,229,230],{"class":82},"        return",[69,232,233],{"class":93}," ORMGetSession",[69,235,143],{"class":86},[69,237,184],{"class":93},[69,239,240],{"class":86},"();\n",[69,242,244,247,250],{"class":71,"line":243},4,[69,245,246],{"class":86},"    } ",[69,248,249],{"class":82},"else",[69,251,252],{"class":86}," {\n",[69,254,256,258,260,262,265,267,269],{"class":71,"line":255},5,[69,257,230],{"class":82},[69,259,233],{"class":93},[69,261,143],{"class":86},[69,263,264],{"class":93},"getActualSession",[69,266,143],{"class":86},[69,268,184],{"class":93},[69,270,240],{"class":86},[69,272,274],{"class":71,"line":273},6,[69,275,276],{"class":86},"    }\n",[69,278,280],{"class":71,"line":279},7,[69,281,282],{"class":86},"}\n",[15,284,285],{},"That little beauty works",[287,288,289,293],"ol",{},[290,291,292],"li",{},"across CFML engines from Lucee to Adobe ColdFusion",[290,294,295],{},"across multiple Hibernate versions from v3.x through v5.x",[15,297,298,299,302],{},"Now, to be fair, I haven't really stress-tested this. It's ",[19,300,301],{},"possible"," there are a few situations where Hibernate doesn't yet know a transaction has begun. But so far... it works, and works well!",[15,304,305],{},"Until next time!",[307,308,309],"style",{},"html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}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 .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":64,"searchDepth":79,"depth":79,"links":311},[312],{"id":12,"depth":227,"text":13},"2021-08-12T16:13:57-04:00",false,"md",{},true,"\u002Fposts\u002Ftransaction-detection-hibernate",{"title":5,"description":64},"posts\u002Ftransaction-detection-hibernate",[322,323,324,325],"cfml","coldfusion","hibernate","orm","nEZJVCgtpksmVqWCAPzUlmKhuzIWJLjWe-xnzR1stdk",1790471527456]