Skip to content

Commit

Permalink
Migrate from atomikos to narayana
Browse files Browse the repository at this point in the history
Removed Atomikos dependencies and code from pom.xml
Added narayana-jta-jakarta:5.13.1.Final dependency to pom.xml
Removed not needed properties from mybatis-config_jta.xml
Added NarayanaDataSourceWrapper which is needed to wrap a XADataSource
Added a primary key to CreateDB_JTA.sql to avoid tests sometimes failing
Changed JtaDatasourceFactory to use Narayana
  • Loading branch information
TheOnlyAl committed Feb 3, 2023
1 parent ffe5256 commit 33733a5
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 45 deletions.
20 changes: 4 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2013-2022 the original author or authors.
Copyright 2013-2023 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -139,19 +139,11 @@
<scope>test</scope>
</dependency>

<!-- Legacy support for atomikos -->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>javax.transaction-api</artifactId>
<version>1.3</version>
<scope>provided</scope>
</dependency>

<!-- Database -->
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jdbc</artifactId>
<version>5.0.9</version>
<groupId>org.jboss.narayana.jta</groupId>
<artifactId>narayana-jta-jakarta</artifactId>
<version>5.13.1.Final</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -199,10 +191,6 @@
<name>derby.stream.error.file</name>
<value>${project.build.directory}/derby.log</value>
</property>
<property>
<name>com.atomikos.icatch.log_base_dir</name>
<value>${project.build.directory}</value>
</property>
</systemProperties>
</configuration>
<executions>
Expand Down
31 changes: 13 additions & 18 deletions src/test/java/org/mybatis/cdi/JtaDatasourceFactory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2022 the original author or authors.
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,36 +15,31 @@
*/
package org.mybatis.cdi;

import com.atomikos.jdbc.AtomikosDataSourceBean;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.derby.jdbc.EmbeddedXADataSource;
import org.apache.ibatis.datasource.DataSourceFactory;

public class JtaDatasourceFactory implements DataSourceFactory {

AtomikosDataSourceBean ds;
private final DataSource dataSource;
private final EmbeddedXADataSource embeddedXADataSource;

public JtaDatasourceFactory() {
this.ds = new AtomikosDataSourceBean();
embeddedXADataSource = new EmbeddedXADataSource();
dataSource = new NarayanaDataSourceWrapper(embeddedXADataSource);
}

@Override
public void setProperties(Properties props) {
this.ds.setUniqueResourceName(props.getProperty("resourceName"));
props.remove("resourceName");
this.ds.setXaDataSourceClassName(props.getProperty("driver"));
props.remove("driver");
this.ds.setMaxPoolSize(Integer.parseInt(props.getProperty("maxPoolSize")));
props.remove("maxPoolSize");
this.ds.setXaProperties(props);
public DataSource getDataSource() {
return dataSource;
}

@Override
public DataSource getDataSource() {
return this.ds;
public void setProperties(final Properties properties) {
embeddedXADataSource.setCreateDatabase(properties.getProperty("createDatabase"));
embeddedXADataSource.setDatabaseName(properties.getProperty("databaseName"));
embeddedXADataSource.setDataSourceName(properties.getProperty("resourceName"));
}

}
}
93 changes: 93 additions & 0 deletions src/test/java/org/mybatis/cdi/NarayanaDataSourceWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.cdi;

import com.arjuna.ats.internal.jdbc.ConnectionManager;
import com.arjuna.ats.jdbc.TransactionalDriver;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.logging.Logger;

import javax.annotation.Nonnull;
import javax.sql.DataSource;
import javax.sql.XADataSource;

public class NarayanaDataSourceWrapper implements DataSource {
private final XADataSource xaDataSource;

public NarayanaDataSourceWrapper(@Nonnull final XADataSource xaDataSource) {
this.xaDataSource = xaDataSource;
}

@Override
public Connection getConnection() throws SQLException {
final var properties = new Properties();
properties.put(TransactionalDriver.XADataSource, xaDataSource);
return ConnectionManager.create(null, properties);
}

@Override
public Connection getConnection(final String username, final String password) throws SQLException {
final var properties = new Properties();
properties.put(TransactionalDriver.XADataSource, xaDataSource);
properties.put(TransactionalDriver.userName, username);
properties.put(TransactionalDriver.password, password);
return ConnectionManager.create(null, properties);
}

@Override
public int getLoginTimeout() throws SQLException {
return xaDataSource.getLoginTimeout();
}

@Override
public PrintWriter getLogWriter() throws SQLException {
return xaDataSource.getLogWriter();
}

@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return xaDataSource.getParentLogger();
}

@Override
public boolean isWrapperFor(final Class<?> iface) throws SQLException {
return iface.isAssignableFrom(getClass());
}

@Override
public void setLoginTimeout(final int seconds) throws SQLException {
xaDataSource.setLoginTimeout(seconds);
}

@Override
public void setLogWriter(final PrintWriter out) throws SQLException {
xaDataSource.setLogWriter(out);
}

@SuppressWarnings("unchecked")
@Override
public <T> T unwrap(final Class<T> iface) throws SQLException {
if (isWrapperFor(iface)) {
return (T) this;
}
throw new SQLException(getClass() + " is not a wrapper for " + iface);
}
}
7 changes: 2 additions & 5 deletions src/test/java/org/mybatis/cdi/UserTransactionProvider.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2022 the original author or authors.
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,8 +15,6 @@
*/
package org.mybatis.cdi;

import com.atomikos.icatch.jta.UserTransactionManager;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.transaction.UserTransaction;
Expand All @@ -26,7 +24,6 @@ public class UserTransactionProvider {
@Produces
@ApplicationScoped
public UserTransaction initTX() {
return (UserTransaction) new UserTransactionManager();
return com.arjuna.ats.jta.UserTransaction.userTransaction();
}

}
5 changes: 3 additions & 2 deletions src/test/resources/org/mybatis/cdi/CreateDB_JTA.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--
-- Copyright 2013-2022 the original author or authors.
-- Copyright 2013-2023 the original author or authors.
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,8 @@

CREATE TABLE USERS (
id integer not null,
name varchar(80) not null
name varchar(80) not null,
PRIMARY KEY(id)
);

insert into users (id, name) values(1, '1-User1');
Expand Down
6 changes: 2 additions & 4 deletions src/test/resources/org/mybatis/cdi/mybatis-config_jta.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright 2013-2022 the original author or authors.
Copyright 2013-2023 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -26,11 +26,9 @@
<property name="" value="" />
</transactionManager>
<dataSource type="org.mybatis.cdi.JtaDatasourceFactory">
<property name="driver" value="org.apache.derby.jdbc.EmbeddedXADataSource" />
<property name="resourceName" value="unique" />
<property name="maxPoolSize" value="1" />
<property name="databaseName" value="memory:dbJTA" />
<property name="createDatabase" value="create" />
<property name="resourceName" value="unique" />
</dataSource>
</environment>
</environments>
Expand Down

0 comments on commit 33733a5

Please sign in to comment.