📄 关于threadlocal用法的一点疑惑1.mht
字号:
style=3D"COLOR: #000000">(</SPAN><SPAN=20
style=3D"COLOR: #aaaadd" ?>System</SPAN>.<SPAN=20
style=3D"COLOR: #000000">err</SPAN><SPAN=20
style=3D"COLOR: #000000">)</SPAN>; <BR> =
=20
=
=20
config =3D <SPAN=20
style=3D"FONT-WEIGHT: bold; COLOR: #990066" =
?>null</SPAN>;=20
<BR> =
=20
=
sessionFactory =3D=20
<SPAN style=3D"FONT-WEIGHT: bold; COLOR: =
#990066"=20
?>null</SPAN>; <BR> =
=20
<SPAN=20
style=3D"COLOR: #000000">}</SPAN> =
<BR> =20
<SPAN style=3D"COLOR: =
#000000">}</SPAN>=20
<BR> <BR> =
=20
<SPAN style=3D"FONT-WEIGHT: bold; COLOR: =
#990066"=20
?>public</SPAN> <SPAN=20
style=3D"FONT-WEIGHT: bold; COLOR: #990066"=20
?>static</SPAN> Session getSession<SPAN=20
style=3D"COLOR: #000000">(</SPAN><SPAN=20
style=3D"COLOR: #000000">)</SPAN> <SPAN=20
style=3D"FONT-WEIGHT: bold; COLOR: #990066"=20
?>throws</SPAN> HibernateException <SPAN=20
style=3D"COLOR: #000000">{</SPAN> =
<BR> =20
=
Session=20
session =3D <SPAN=20
style=3D"COLOR: #000000">(</SPAN>Session<SPAN=20
style=3D"COLOR: =
#000000">)</SPAN>sessionContext.<SPAN=20
style=3D"COLOR: #000000">get</SPAN><SPAN=20
style=3D"COLOR: #000000">(</SPAN><SPAN=20
style=3D"COLOR: #000000">)</SPAN>; <BR> =
=20
<SPAN=20
style=3D"FONT-WEIGHT: bold; COLOR: #990066"=20
?>if</SPAN><SPAN style=3D"COLOR: =
#000000">(</SPAN>session=20
=3D=3D <SPAN style=3D"FONT-WEIGHT: bold; COLOR: =
#990066"=20
?>null</SPAN><SPAN style=3D"COLOR: =
#000000">)</SPAN><SPAN=20
style=3D"COLOR: #000000">{</SPAN> <BR> =
=20
=
=20
session =3D sessionFactory.<SPAN=20
style=3D"COLOR: #000000">openSession</SPAN><SPAN =
style=3D"COLOR: #000000">(</SPAN><SPAN=20
style=3D"COLOR: #000000">)</SPAN>; =
=20
<BR> =
=20
=
sessionContext.<SPAN=20
style=3D"COLOR: #000000">set</SPAN><SPAN=20
style=3D"COLOR: #000000">(</SPAN>session<SPAN=20
style=3D"COLOR: #000000">)</SPAN>; <BR> =
=20
<SPAN=20
style=3D"COLOR: #000000">}</SPAN> <BR> =
=20
<SPAN=20
style=3D"FONT-WEIGHT: bold; COLOR: #990066"=20
?>return</SPAN> session; <BR> =
=20
<SPAN style=3D"COLOR: =
#000000">}</SPAN> =20
<BR> <BR> =
=20
<SPAN style=3D"FONT-WEIGHT: bold; COLOR: =
#990066"=20
?>public</SPAN> <SPAN=20
style=3D"FONT-WEIGHT: bold; COLOR: #990066"=20
?>static</SPAN> <SPAN=20
style=3D"FONT-WEIGHT: bold; COLOR: #990066" =
?>void</SPAN>=20
closeSession<SPAN style=3D"COLOR: =
#000000">(</SPAN><SPAN=20
style=3D"COLOR: #000000">)</SPAN> <SPAN=20
style=3D"FONT-WEIGHT: bold; COLOR: #990066"=20
?>throws</SPAN> HibernateException<SPAN=20
style=3D"COLOR: #000000">{</SPAN> <BR> =
=20
Session =
session =3D=20
<SPAN style=3D"COLOR: =
#000000">(</SPAN>Session<SPAN=20
style=3D"COLOR: =
#000000">)</SPAN>sessionContext.<SPAN=20
style=3D"COLOR: #000000">get</SPAN><SPAN=20
style=3D"COLOR: #000000">(</SPAN><SPAN=20
style=3D"COLOR: #000000">)</SPAN>; <BR> =
=20
=20
sessionContext.<SPAN=20
style=3D"COLOR: #000000">set</SPAN><SPAN=20
style=3D"COLOR: #000000">(</SPAN><SPAN=20
style=3D"FONT-WEIGHT: bold; COLOR: #990066"=20
?>null</SPAN><SPAN style=3D"COLOR: =
#000000">)</SPAN>;=20
<BR> =
=20
<SPAN style=3D"FONT-WEIGHT: bold; COLOR: =
#990066"=20
?>if</SPAN><SPAN style=3D"COLOR: =
#000000">(</SPAN>session=20
!=3D <SPAN style=3D"FONT-WEIGHT: bold; COLOR: =
#990066"=20
?>null</SPAN><SPAN style=3D"COLOR: =
#000000">)</SPAN><SPAN=20
style=3D"COLOR: #000000">{</SPAN> <BR> =
=20
=
=20
session.<SPAN=20
style=3D"COLOR: #000000">close</SPAN><SPAN=20
style=3D"COLOR: #000000">(</SPAN><SPAN=20
style=3D"COLOR: #000000">)</SPAN>; <BR> =
=20
<SPAN=20
style=3D"COLOR: #000000">}</SPAN> <BR> =
=20
<SPAN style=3D"COLOR: #000000">}</SPAN> =
<BR> =20
<BR> =
=20
<BR> <SPAN=20
style=3D"FONT-WEIGHT: bold; COLOR: #990066"=20
?>public</SPAN> <SPAN=20
style=3D"FONT-WEIGHT: bold; COLOR: #990066"=20
?>static</SPAN> Configuration getConfig<SPAN=20
style=3D"COLOR: #000000">(</SPAN><SPAN=20
style=3D"COLOR: #000000">)</SPAN><SPAN=20
style=3D"COLOR: #000000">{</SPAN> =
<BR> =20
=
<SPAN=20
style=3D"FONT-WEIGHT: bold; COLOR: #990066"=20
?>return</SPAN> config; <BR> =
=20
<SPAN style=3D"COLOR: =
#000000">}</SPAN> =20
<BR> <BR> =
=20
<SPAN style=3D"FONT-WEIGHT: bold; COLOR: =
#990066"=20
?>public</SPAN> <SPAN=20
style=3D"FONT-WEIGHT: bold; COLOR: #990066"=20
?>static</SPAN> SessionFactory =
getSessionFactory<SPAN=20
style=3D"COLOR: #000000">(</SPAN><SPAN=20
style=3D"COLOR: #000000">)</SPAN><SPAN=20
style=3D"COLOR: #000000">{</SPAN> =
<BR> =20
=
<SPAN=20
style=3D"FONT-WEIGHT: bold; COLOR: #990066"=20
?>return</SPAN> sessionFactory; <BR> =
=20
<SPAN style=3D"COLOR: =
#000000">}</SPAN>=20
<BR> <BR><SPAN=20
style=3D"COLOR: =
#000000">}</SPAN></DIV><BR></TD></TR></TBODY></TABLE><SPAN=20
=
class=3Dpostbody><BR><BR>=D4=DA=D2=BB=B8=F6jsp=D2=B3=C3=E6=D6=D0=A3=AC=C8=
=E7=B9=FB=CE=D2=C4=B3=B4=A6=B5=F7=D3=C3=C1=CB=C0=E0foo=B5=C4=BA=AF=CA=FDm=
ethod=A3=AC=20
<BR></SPAN>
<TABLE cellSpacing=3D1 cellPadding=3D3 width=3D"90%" =
align=3Dcenter=20
border=3D0>
<TBODY>
<TR>
<TD><SPAN =
class=3Dgenmed><B>java=B4=FA=C2=EB: </B></SPAN></TD></TR>
<TR>
<TD class=3Dcode>
<DIV=20
style=3D"FONT-FAMILY: 'Courier New', Courier, =
monospace"><BR><%=20
<BR>foo.<SPAN style=3D"COLOR: =
#000000">method</SPAN><SPAN=20
style=3D"COLOR: #000000">(</SPAN><SPAN=20
style=3D"COLOR: #000000">)</SPAN>;=20
<BR>%></DIV><BR></TD></TR></TBODY></TABLE><SPAN=20
class=3Dpostbody><BR><SPAN=20
style=3D"FONT-WEIGHT: =
bold">=CE=D2=C8=CF=CE=AAfoo.method()=D3=A6=B8=C3=D5=E2=D1=F9=D3=C3ThreadL=
ocal:</SPAN>=20
<BR>public void method() throws HibernateException{=20
<BR>ThreadLocalSession.getSession(); <BR>//to do =
something...=20
<BR>//<SPAN=20
style=3D"FONT-WEIGHT: =
bold">=B4=CB=B4=A6=B2=A2=B2=BB=B5=F7=D3=C3=B9=D8=B1=D5ThreadLocalSession.=
closeSession();</SPAN>=20
<BR>} =
<BR><BR>=B6=F8=CA=C7=D4=DA=D2=BB=B8=F6jsp=D2=B3=C3=E6=D6=D0=C0=B4=B9=DC=C0=
=EDThreadLocalSession=A3=AC=BD=E1=B9=B9=C8=E7=CF=C2=A3=BA=20
<BR></SPAN>
<TABLE cellSpacing=3D1 cellPadding=3D3 width=3D"90%" =
align=3Dcenter=20
border=3D0>
<TBODY>
<TR>
<TD><SPAN =
class=3Dgenmed><B>java=B4=FA=C2=EB: </B></SPAN></TD></TR>
<TR>
<TD class=3Dcode>
<DIV=20
style=3D"FONT-FAMILY: 'Courier New', Courier, =
monospace"><BR><%=20
<BR>Session sess =3D ThreadLocalSessin.<SPAN=20
style=3D"COLOR: #000000">getSession</SPAN><SPAN=20
style=3D"COLOR: #000000">(</SPAN><SPAN=20
style=3D"COLOR: #000000">)</SPAN>; =
<BR>Transaction tx;=20
<BR><SPAN style=3D"FONT-WEIGHT: bold; COLOR: =
#990066"=20
?>try</SPAN> <SPAN style=3D"COLOR: =
#000000">{</SPAN>=20
<BR> tx =3D sess.<SPAN=20
style=3D"COLOR: =
#000000">beginTransaction</SPAN><SPAN=20
style=3D"COLOR: #000000">(</SPAN><SPAN=20
style=3D"COLOR: #000000">)</SPAN>; <BR> =
=20
<SPAN style=3D"COLOR: #6666ff">//do some =
work</SPAN>=20
<BR> ... <BR> =20
<SPAN style=3D"COLOR: =
#000000">tx</SPAN>.<SPAN=20
style=3D"COLOR: #000000">commit</SPAN><SPAN=20
style=3D"COLOR: #000000">(</SPAN><SPAN=20
style=3D"COLOR: #000000">)</SPAN>; <BR><SPAN=20
style=3D"COLOR: #000000">}</SPAN> <BR><SPAN=20
style=3D"FONT-WEIGHT: bold; COLOR: #990066" =
?>catch</SPAN>=20
<SPAN style=3D"COLOR: #000000">(</SPAN><SPAN=20
style=3D"COLOR: #aaaadd" ?>Exception</SPAN> =
e<SPAN=20
style=3D"COLOR: #000000">)</SPAN> <SPAN=20
style=3D"COLOR: #000000">{</SPAN> <BR> =
=20
<SPAN style=3D"FONT-WEIGHT: bold; COLOR: =
#990066"=20
?>if</SPAN> <SPAN=20
style=3D"COLOR: #000000">(</SPAN>tx!=3D<SPAN=20
style=3D"FONT-WEIGHT: bold; COLOR: #990066"=20
?>null</SPAN><SPAN style=3D"COLOR: =
#000000">)</SPAN>=20
tx.<SPAN style=3D"COLOR: =
#000000">rollback</SPAN><SPAN=20
style=3D"COLOR: #000000">(</SPAN><SPAN=20
style=3D"COLOR: #000000">)</SPAN>; <BR> =
=20
throw e; <BR><SPAN style=3D"COLOR: =
#000000">}</SPAN>=20
<BR><SPAN style=3D"FONT-WEIGHT: bold; COLOR: =
#990066"=20
?>finally</SPAN> <SPAN style=3D"COLOR: =
#000000">{</SPAN>=20
<BR> ThreadLocalSession.<SPAN =
style=3D"COLOR: =
#000000">closeSession</SPAN><SPAN=20
style=3D"COLOR: #000000">(</SPAN><SPAN=20
style=3D"COLOR: #000000">)</SPAN>; <BR><SPAN=20
style=3D"COLOR: #000000">}</SPAN>=20
<BR>%></DIV><BR></TD></TR></TBODY></TABLE><SPAN=20
class=3Dpostbody><BR><BR><SPAN=20
style=3D"FONT-WEIGHT: =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -