设为首页 收藏本站
查看: 599|回复: 0

[经验分享] How to use BTM as the transaction manager in Tomcat 6.x

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-8-10 08:21:35 | 显示全部楼层 |阅读模式
  原文 http://docs.codehaus.org/display/BTM/Tomcat13
  These instructions have been verified against BTM 1.3.1.
Contents


  • Step 1: Copy the BTM jars
  • Step 2: Configure BTM as the transaction manager
  • Step 3: Configure datasources that are transaction aware
  • Step 4: Configure transaction manager and datasources initialization in your META-INF/context.xml
  • Step 5: Configure datasources references in your web.xml
Step 1: Copy the BTM jars
  Copy the following jars from the BTM distribution to the Tomcat lib/ directory:

  • btm-1.3.1.jar
  • geronimo-jta_1.0.1B_spec-1.0.1.jar
  • slf4j-api-1.5.2.jar
  • slf4j-jdk14-1.5.2.jar
  • btm-tomcat55-lifecycle.jar (it works with both Tomcat 5.5 and Tomcat 6)
  You will also need to copy your JDBC driver's JAR file in that same folder. In this example, we've used Derby 10.3.2.1 so we copied derby.jar in Tomcat's lib/ directory.
Step 2: Configure BTM as the transaction manager
  Windows: Create a file named setenv.bat with the following commands under Tomcat's bin/ directory:






set CATALINA_OPTS=-Dbtm.root=%CATALINA_HOME% -Dbitronix.tm.configuration=%CATALINA_HOME%\conf\btm-config.properties
  Unix: Create a file named setenv.sh with the following commands under Tomcat's bin/ directory:






CATALINA_OPTS="-Dbtm.root=$CATALINA_HOME -Dbitronix.tm.configuration=$CATALINA_HOME/conf/btm-config.properties"
  Now create a file named btm-config.properties with the following properties under Tomcat's conf/ directory:






bitronix.tm.serverId=tomcat-btm-node0
bitronix.tm.journal.disk.logPart1Filename=${btm.root}/work/btm1.tlog
bitronix.tm.journal.disk.logPart2Filename=${btm.root}/work/btm2.tlog
bitronix.tm.resource.configuration=${btm.root}/conf/resources.properties
  Then edit the file named server.xml under Tomcat's conf/ directory. Under this line:






  add this one:






  The  tag will make sure BTM is started when Tomcat starts up and shutdown when Tomcat shuts down.
  The next step is to edit the file named context.xml under Tomcat's conf/ directory. Under this line:






WEB-INF/web.xml
  add this one:






  The  tag will bind the transaction manager at the standard JNDI location java:comp/UserTransaction.
  Finally, create an empty file named resources.properties under Tomcat's conf/ directory.
Step 3: Configure datasources that are transaction aware
  You have to put your datasources configurations in Tomcat's conf/resources.properties file. Here's an example of using BTM with a DataSource that implements javax.sql.XADataSource:






resource.ds1.className=org.apache.derby.jdbc.EmbeddedXADataSource
resource.ds1.uniqueName=jdbc/mydatasource
resource.ds1.minPoolSize=0
resource.ds1.maxPoolSize=5
resource.ds1.driverProperties.databaseName=../work/db1
resource.ds1.driverProperties.createDatabase=create
  This will create a bitronix.tm.resource.jdbc.PoolingDataSource that implements javax.sql.DataSource and interacts with the javax.sql.XADataSource provided in this instance by Derby.
  If your database vendor does not provide an XADataSource, you can use BTM's bitronix.tm.resource.jdbc.lrc.LrcXADataSource as the XADataSource to allow your database connections to be controlled by the transaction manager:






resource.ds2.className=bitronix.tm.resource.jdbc.lrc.LrcXADataSource
resource.ds2.uniqueName=jdbc/exampleNonXADS
resource.ds2.minPoolSize=0
resource.ds2.maxPoolSize=5
resource.ds2.driverProperties.driverClassName=org.apache.derby.jdbc.EmbeddedDriver
resource.ds2.driverProperties.url=jdbc:derby:../work/db2;create=true
  Again, we've used Derby as an example, but as the LrcXADataSource uses only the class name and url of a java.sql.Driver, you can use it with any database providing a JDBC driver.
Step 4: Configure transaction manager and datasources initialization in your META-INF/context.xml
  In the web application where you want one or more datasource to be used, you have to create a META-INF/context.xml file.








   

   

  The  tags will bind a bitronix.tm.resource.ResourceObjectFactory object each, passing it a javax.naming.Reference containing a javax.naming.StringRefAddr containing the datasource's uniqueName as addrType.

http://docs.codehaus.org/images/icons/emoticons/information.pngTomcat specific
  This mechanism is internal to Tomcat. You do not have to worry about how it works, the bitronix.tm.resource.ResourceObjectFactory class will handle those details.
  The bitronix.tm.resource.ResourceObjectFactory class will return the datasource previously configured in in Tomcat's conf/resources.properties with the specified uniqueName when it is fetched from JNDI.
Step 5: Configure datasources references in your web.xml
  Before your code can access configured datasources via JNDI ENC URLs, you need to declare resource references in your web.xml:










  
    jdbc/mydatasource
    javax.sql.DataSource
  

  
    jdbc/exampleNonXADS
    javax.sql.DataSource
  
  Now you can do JNDI lookups on those URLs to access the configured datasources:






DataSource exampleNonXADS = (DataSource) ctx.lookup("java:comp/env/jdbc/exampleNonXADS");
DataSource mydatasource = (DataSource) ctx.lookup("java:comp/env/jdbc/mydatasource");
  and you can do JNDI lookups on this URL to access the transaction manager:






UserTransaction ut = (UserTransaction) ctx.lookup("java:comp/UserTransaction");
http://www.bitronix.be/images/shim.gif

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-96715-1-1.html 上篇帖子: Tomcat中配置MySQL数据库连接池 下篇帖子: Tomcat源码学习(15)-How Tomcat works(转)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表