博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to use BTM as the transaction manager in Tomcat 6.x
阅读量:4538 次
发布时间:2019-06-08

本文共 5891 字,大约阅读时间需要 19 分钟。

原文

These instructions have been verified against BTM 1.3.1.

Contents

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:

<Listener className=
"org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"
/>

add this one:

<Listener className=
"bitronix.tm.integration.tomcat55.BTMLifecycleListener"
/>

The <Listener> 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:

<WatchedResource>WEB-INF/web.xml</WatchedResource>

add this one:

<Transaction factory=
"bitronix.tm.BitronixUserTransactionObjectFactory"
/>

The <Transaction> 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 .

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 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 file.

<
Context
>
 
    
<
Resource
name
=
"jdbc/mydatasource"
auth
=
"Container"
type
=
"javax.sql.DataSource"
        
factory
=
"bitronix.tm.resource.ResourceObjectFactory"
uniqueName
=
"jdbc/mydatasource"
/>
 
    
<
Resource
name
=
"jdbc/exampleNonXADS"
auth
=
"Container"
type
=
"javax.sql.DataSource"
        
factory
=
"bitronix.tm.resource.ResourceObjectFactory"
uniqueName
=
"jdbc/exampleNonXADS"
/>
 
</
Context
>

The <Resource> tags will bind a object each, passing it a containing a containing the datasource's uniqueName as addrType.

Tomcat 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:

<?
xml
version
=
"1.0"
encoding
=
"ISO-8859-1"
?>
 
<!DOCTYPE web-app PUBLIC
    
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    
">
 
<
web-app
>
  
<
resource-env-ref
>
    
<
resource-env-ref-name
>jdbc/mydatasource</
resource-env-ref-name
>
    
<
resource-env-ref-type
>javax.sql.DataSource</
resource-env-ref-type
>
  
</
resource-env-ref
>
 
  
<
resource-env-ref
>
    
<
resource-env-ref-name
>jdbc/exampleNonXADS</
resource-env-ref-name
>
    
<
resource-env-ref-type
>javax.sql.DataSource</
resource-env-ref-type
>
  
</
resource-env-ref
>
</
web-app
>

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"
);

posted on
2013-02-16 15:44 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/lonelyxmas/archive/2013/02/16/2913443.html

你可能感兴趣的文章
Ubuntu 18.04 启用 rc.local 设置开机启动
查看>>
Single Number
查看>>
PostgreSQL之时间戳自动更新
查看>>
常用文件后缀名与打开方式
查看>>
怎么读取照片内的文字
查看>>
php 发送邮件代码
查看>>
execCommand()命令详解及实例展示
查看>>
NSString NSCFString区别
查看>>
C#winform自定义控件模拟设计时界面鼠标移动和调节大小、选中效果
查看>>
DIV非偏移放大
查看>>
Jquery 展开收起
查看>>
css的选择器优先级
查看>>
C#快捷键
查看>>
linux下安装mysql
查看>>
selenium+Python(处理html5的视频播放)
查看>>
在Winform开发中使用FastReport创建报表
查看>>
C# 反射研究
查看>>
[原创][Verilog]个人.v文件书写规范
查看>>
[原创]Matlab获取当前时间信息
查看>>
Bulma 中的媒体查询
查看>>