﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-放松人生，追求完美-随笔分类-事务管理</title><link>http://www.blogjava.net/hx9111/category/1705.html</link><description>生如夏花，死如秋叶 
生如夏花之绚烂，死如秋叶之静美</description><language>zh-cn</language><lastBuildDate>Fri, 02 Mar 2007 06:41:43 GMT</lastBuildDate><pubDate>Fri, 02 Mar 2007 06:41:43 GMT</pubDate><ttl>60</ttl><item><title>事务的理解和jotm，xapool的使用 （1）</title><link>http://www.blogjava.net/hx9111/archive/2005/06/11/5917.html</link><dc:creator>我爱夏花,更爱秋叶</dc:creator><author>我爱夏花,更爱秋叶</author><pubDate>Sat, 11 Jun 2005 10:13:00 GMT</pubDate><guid>http://www.blogjava.net/hx9111/archive/2005/06/11/5917.html</guid><wfw:comment>http://www.blogjava.net/hx9111/comments/5917.html</wfw:comment><comments>http://www.blogjava.net/hx9111/archive/2005/06/11/5917.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hx9111/comments/commentRss/5917.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hx9111/services/trackbacks/5917.html</trackback:ping><description><![CDATA[<SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"></SPAN><SPAN lang=EN-US><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">对于中小型的应用</SPAN><SPAN lang=EN-US>tomcat</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">作为服务器就足够了，但是，在我把框架往</SPAN><SPAN lang=EN-US>tomcat</SPAN></FONT><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><FONT size=2>上转移的时候有了一个问题，工作流引擎的数据库是独立的，如何保证他和业务的数据库事务上的统一性，这里就涉及到分布式事务的概念。<BR><BR></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">像</SPAN><SPAN lang=EN-US>weblogic</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，</SPAN><SPAN lang=EN-US>websphere</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，这种企业级服务器，他们有自己的事务管理器，你可以配置多个</SPAN><SPAN lang=EN-US>datasource</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，这些</SPAN><SPAN lang=EN-US>datasource</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">可以指向不同的资源（数据库，消息服务），事务管理器就是这些资源的管理中心，当一个事务开始的时候（</SPAN><SPAN lang=EN-US>begin</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">），事务管理器会记录并监视这个事务涉及的所有可管理资源，当一个事务结束的时候（</SPAN><SPAN lang=EN-US>commit</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">），他会把所有的资源提交，而当程序出现异常的时候，他会把所有的资源回滚（</SPAN><SPAN lang=EN-US>rollback</SPAN></FONT><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><FONT size=2>）。在事务边界以内，所有的可管理资源实际都是没有提交的，处于一种等待状态，只有当事务提交的时候，事务管理器才负责把它所管理的所有资源提交。事务管理器就是一个全局事务管理中心，它负责把许多可管理（可以控制事务）的资源的事务统一起来。<BR><BR></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">出于这种这种考虑，我在管理全局性事务的时候，选择了</SPAN><SPAN lang=EN-US>jtom</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US>xapool</SPAN></FONT><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><FONT size=2>。<BR><BR></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><FONT size=2><SPAN lang=EN-US>Jotm</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是一个开源的</SPAN><SPAN lang=EN-US>JTA</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">实现，是由</SPAN><SPAN lang=EN-US>ObjectWeb</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">组织开发的，实际就是实现了事务管理器的功能，而且他还支持分布式事务，如果把</SPAN><SPAN lang=EN-US>jotm</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">结合</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">JORAM </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">（也是由</SPAN><SPAN lang=EN-US>ObjectWeb</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">组织开发的</SPAN><SPAN lang=EN-US>JMS</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">实现<SPAN style="mso-bidi-font-weight: bold">）使用，就可以实现</SPAN></SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">JMS</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">的事务管理。（这里我在想，</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">JBossCache</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">是支持事务的</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">cache</SPAN></FONT><FONT size=2><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">，如果把它们结合在一起，是不是就可以对缓存进行事务控制了：））<BR><BR></SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold"><o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">对于</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">jotm</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">的使用，你只要记住这个应用中只有一个</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">Jotm</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">对象就</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">OK</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">了，对于分布式事务也是一样，如何保证一个应用中只用一个</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">Jotm</SPAN></FONT><FONT size=2><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">实例呢？<BR><BR></SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold"><o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 42pt; mso-char-indent-count: 4.0"><SPAN lang=EN-US style="mso-bidi-font-weight: bold"><FONT size=2><FONT color=#a52a2a>jotmCurrent = Current.getCurrent();</FONT><BR><BR><o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=2><SPAN lang=EN-US style="mso-bidi-font-weight: bold"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">使用上面的方法，如果</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">jotmCurrent </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">等于</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">null</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">，说明现在的</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">jvm</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">中没有没有</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">jotm</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">实例，当需要分布式的时候就不能这么判断了，你必须把</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">jotm</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">对象放到</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">jndi</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">上，以后使用的时候从</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">jndi</SPAN></FONT><FONT size=2><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">上取就可以了。<BR><BR></SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold"><o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=2><SPAN lang=EN-US style="mso-bidi-font-weight: bold"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">如果</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">jotmCurrent</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">不等于空，说明</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">jvm</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">中已经有</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">jotm</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">实例了，而如何得到这个实例了，如果从这个角度去考虑，是不行的，你可以看一下</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">Jotm</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">的</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">API</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">，看一下</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">Current</SPAN></FONT><FONT size=2><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">的类说明：<BR><BR></SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold"><o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-bidi-font-weight: bold"><SPAN style="mso-spacerun: yes"><FONT size=2>&nbsp;<FONT color=#0000ff>&nbsp; </FONT></FONT></SPAN><A href="http://jotm.objectweb.org/current/jotm/jdoc/"><FONT color=#0000ff size=2>http://jotm.objectweb.org/current/jotm/jdoc/</FONT></A><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-bidi-font-weight: bold"><o:p><FONT size=2>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=2><SPAN lang=EN-US style="mso-bidi-font-weight: bold"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN lang=EN-US>public class <B>Current</B> </SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 31.5pt; mso-char-indent-count: 3.0"><SPAN lang=EN-US><FONT size=2>extends </FONT><A href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html"><FONT color=#0000ff size=2>Object</FONT></A><FONT size=2> </FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 36.75pt; mso-char-indent-count: 3.5"><SPAN lang=EN-US><FONT size=2>implements </FONT><A href="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/transaction/UserTransaction.html"><FONT color=#0000ff size=2>UserTransaction</FONT></A><FONT color=#0000ff size=2>, </FONT><A href="http://jotm.objectweb.org/current/jotm/jdoc/org/objectweb/transaction/jta/TransactionManager.html"><FONT color=#0000ff size=2>TransactionManager</FONT></A><FONT color=#0000ff size=2>, </FONT><A href="http://java.sun.com/j2se/1.3/docs/api/javax/naming/Referenceable.html"><FONT color=#0000ff size=2>Referenceable</FONT></A><FONT color=#0000ff size=2>, </FONT><A href="http://java.sun.com/j2se/1.3/docs/api/java/io/Serializable.html"><FONT color=#0000ff size=2>Serializable</FONT></A><FONT size=2> <BR><BR></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=2><SPAN lang=EN-US style="mso-bidi-font-weight: bold">Current </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">对象实际就是一个事务管理器，哈哈，我们使用</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">jotm</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">，不就是为了这个吗，</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">ok</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">，</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">you got it</SPAN></FONT><FONT size=2><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">！<BR><BR></SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold"><o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">我对</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">jotm</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">和事务的研究还不够深入，以上都是个人理解，有不对的地方还请大家指出！</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold"><o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=2><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">下面的文章，我重点对</SPAN><SPAN lang=EN-US style="mso-bidi-font-weight: bold">xapool</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">进行说明（使用他的时候问题特别多：））</SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"></o:p></SPAN>&nbsp;</P><img src ="http://www.blogjava.net/hx9111/aggbug/5917.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hx9111/" target="_blank">我爱夏花,更爱秋叶</a> 2005-06-11 18:13 <a href="http://www.blogjava.net/hx9111/archive/2005/06/11/5917.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>