About Tomcat combined with Atomikos to implement JTA

About Tomcat combined with Atomikos to implement JTA

Recently, the project switched the environment and replaced weblogic with tomcat. I recorded the problems encountered in the middle.
Configuring Atomikos to implement JTA under Tomcat
As a classic web server, Tomcat is widely used in development, testing, and production environments. But Tomcat is not a Java EE server after all, so it does not provide support for EJB and JTA. This article describes a method for Tomcat to implement JTA using Atomikos.

 Using JTA in Tomcat, you can deploy Atomikos in Tomcat and use the data source supported by Tomcat; you can also configure it in the project and use Spring to configure the data source, connection pool, transaction manager, etc. The two methods have their own characteristics. This article only introduces the integration of Tomcat and Atomikos. After the integration, Tomcat can provide JTA transaction manager and data source to the outside world.

         Before using Atomikos, we also used JOTM, but under high concurrency conditions, JOTM frequently failed and we had to give up. Through testing, we found that Atomikos had good performance and stability.

         We used the latest version 4.04 of Atomikos. The Jar package can be obtained from the Maven configuration library. The link address is: http://mvnrepository.com/artifact/com.atomikos

If you do not use Hibernate, the required packages include:

atomikos-util.jar,
jta.jar,
transactions.jar,
transactions-api.jar,
transactions-jdbc.jar,
transactions-jta.jar

Integration Package:
atomikos-integration-extension-3.7.2.jar

Remember to put the database driver

Step 1: Copy these jars to the lib directory of tomcat. To integrate Tomcat with Atomikos, you also need an integration package. This integration package contains two classes. You can refer to the implementation yourself or use the official jar package. The latest one is

atomikos-integration-extension-3.7.2.jar

Step 2: Add a listener in tomcat/config/server.xml

<Listener className="com.atomikos.tomcat.AtomikosLifecycleListener" />

Step 3: Add data sources and related transaction managers in tomcat/config/context.xml. The following is a reference example. Modify the parameters as appropriate.

 <Resource name="jdbc/DS_MYSQL"

            auth="Container"

            type="com.atomikos.jdbc.AtomikosDataSourceBean"

            uniqueResourceName="jdbc/DS_MYSQL"

            xaDataSourceClassName="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"

            xaProperties.databaseName="db_test"

            xaProperties.serverName="localhost"

            xaProperties.port="3306"

            xaProperties.user="root"

            xaProperties.password="root"

            maxPoolSize="200"

            xaProperties.url="jdbc:mysql://localhost:3306/db_test?characterEncoding=UTF8"

            factory="com.atomikos.tomcat.EnhancedTomcatAtomikosBeanFactory" />

  <Resource name="UserTransaction"

            auth="Container"

            type="javax.transaction.UserTransaction" />   

   <Transaction factory="com.atomikos.icatch.jta.UserTransactionFactory" />

Step 4: Add a jta.properties file in the tomcat/lib directory and set the Atomikos transaction-related parameters. Otherwise, the default configuration parameters will be used. Some concurrent transaction numbers (50 by default) and timeouts need to be adjusted. Some parameter configurations in the file are given below. For parameter explanations, please refer to the official documentation: https://www.atomikos.com/Documentation/JtaProperties

Add this line configuration

com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory

The default values ​​of the parameters in Atomikos are defined in transactions.jar, transactions-default.properties: if you are interested, you can go and see it yourself

After configuring the above four steps, the integration of Tomcat is complete. Spring can be used in the project to associate the data source and transaction manager. The reference configuration is as follows:

<!-- JNDI template configuration information, used to connect to the application server -->

<bean class="org.springframework.jndi.JndiTemplate" id="jndiTemplate" />
<bean class="org.springframework.jndi.JndiObjectFactoryBean" id="dataSource">

    <property name="jndiName">

        <value>java:comp/env/jdbc/DS_MYSQL</value>

    </property>

    <property name="jndiTemplate">

        <ref bean="jndiTemplate"/>

    </property>

</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

    <property name="dataSource">

        <ref bean="dataSource" />

    </property>

</bean>  

<!--User transaction object-->

<bean class="org.springframework.jndi.JndiObjectFactoryBean" id="userTransaction">

    <!--class="org.springframework.transaction.jta.WebLogicJtaTransactionManager">-->

    <property name="jndiName">

        <value>java:comp/UserTransaction</value>

    </property>

    <property name="jndiTemplate">

        <ref bean="jndiTemplate"/>

    </property>

</bean>

<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"

    init-method="init" destroy-method="close">

    <property name="forceShutdown" value="false" />

</bean>

<!-- Configure annotation-based declarative transaction manager -->

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">

    <property name="userTransaction" ref="userTransaction" />

    <property name="transactionManager" ref="atomikosTransactionManager" />

</bean> 

<tx:annotation-driven transaction-manager="transactionManager" />

The following is the configuration used in my project: It is recommended to configure in conf.xml

The XA data source and JDBC driver used in the Tomcat configuration can use nonXA related settings. Atomikos also supports non-XA connections to increase the running speed. Regarding the nonXa data source, you can refer to the following configuration:

<Resource name="jdbc/DS_MYSQL"

 auth="Container"
    type="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean"

        uniqueResourceName="jdbc/DS_MYSQL"

        driverClassName="com.mysql.jdbc.Driver"

        maxPoolSize="200"

        url="jdbc:mysql://localhost:3306/db_test?characterEncoding=UTF8"

        user="root"

        password="root"

        factory="com.atomikos.tomcat.EnhancedTomcatAtomikosBeanFactory" />

**Pitfall Record**

**Here, because there is a transaction manager TransactionManager, UserTransaction cannot be obtained through this type. After debugging, it is found that this class is not found**

**Change to type="com.atomikos.icatch.jta.userTransactionImp" to successfully obtain UserTransaction,**

 <Resource name="UserTransaction"

            auth="Container"
type="com.atomikos.icatch.jta.userTransactionImp"/>   

   <Transaction factory="com.atomikos.icatch.jta.UserTransactionFactory" /> 

https://blog.csdn.net/xuyu_yt/article/details/77905553?locationNum=14%20fps=1

This is the end of this article about Tomcat combined with Atomikos to implement JTA. For more relevant content about Atomikos implementing JTA, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Springboot jta atomikos realizes distributed transaction management

<<:  Solve the problem of MySql8.0 checking transaction isolation level error

>>:  Detailed explanation of the use of CSS3 rgb and rgba (transparent color)

Recommend

Detailed explanation of Vue plugin

Summarize This article ends here. I hope it can b...

HTML form and the use of form internal tags

Copy code The code is as follows: <html> &l...

Completely uninstall MySQL database in Windows system to reinstall MySQL

1. In the control panel, uninstall all components...

Installation tutorial of MySQL 5.1 and 5.7 under Linux

The operating system for the following content is...

How to build php-nginx-alpine image from scratch in Docker

Although I have run some projects in Docker envir...

A brief analysis of the difference between static and self in PHP classes

Use self:: or __CLASS__ to get a static reference...

How to use Docker to package and deploy images locally

First time using docker to package and deploy ima...

Overview of MySQL Statistics

MySQL executes SQL through the process of SQL par...

Summary of MySQL injection bypass filtering techniques

First, let’s look at the GIF operation: Case 1: S...

How to set static IP in centOS7 NET mode

Preface NAT forwarding: Simply put, NAT is the us...

A brief discussion on how to use slots in Vue

How to define and use: Use the slot tag definitio...

Workerman writes the example code of mysql connection pool

First of all, you need to understand why you use ...

How to create a trigger in MySQL

This article example shares the specific code for...