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 tutorial on deploying apollo with docker

1. Introduction I won’t go into details about apo...

The magic of tbody tag speeds up the display of table content

You must have saved other people’s web pages and l...

Vue el-date-picker dynamic limit time range case detailed explanation

There are two situations 1. Start time and end ti...

Sample code using scss in uni-app

Pitfalls encountered I spent the whole afternoon ...

Use of marker tags in CSS list model

This article mainly introduces the ::master pseud...

Several ways to hide Html elements

1. Use CSS Copy code The code is as follows: style...

Vue+Echart bar chart realizes epidemic data statistics

Table of contents 1. First install echarts in the...

Detailed tutorial on installing mysql on centos 6.9

1. Confirm whether MySQL has been installed. You ...

Detailed explanation of mysql basic operation statement commands

1. Connect to MySQL Format: mysql -h host address...

Detailed explanation of NodeJS modularity

Table of contents 1. Introduction 2. Main text 2....

Detailed explanation of Vue's custom event content distribution

1. This is a bit complicated to understand, I hop...

Nginx implements https website configuration code example

https base port 443. It is used for something cal...

Four categories of CSS selectors: basic, combination, attribute, pseudo-class

What is a selector? The role of the selector is t...

How to draw the timeline with vue+canvas

This article example shares the specific code of ...