From 8941ee289f6bcf1fffd80651a78d6b43d764f0ff Mon Sep 17 00:00:00 2001 From: Alexander Wienzek Date: Fri, 3 Feb 2023 11:17:40 +0100 Subject: [PATCH] [port] Replace atomikos with narayana with small variation from jakarta branch pull this over to main branch so we are consistently the same. This varies a small bit and uses different artifact to achieve same desired state. --- pom.xml | 14 ++- .../org/mybatis/cdi/JtaDatasourceFactory.java | 28 +++--- .../cdi/NarayanaDataSourceWrapper.java | 92 +++++++++++++++++++ .../mybatis/cdi/UserTransactionProvider.java | 6 +- .../org/mybatis/cdi/CreateDB_JTA.sql | 5 +- .../org/mybatis/cdi/mybatis-config_jta.xml | 6 +- 6 files changed, 117 insertions(+), 34 deletions(-) create mode 100644 src/test/java/org/mybatis/cdi/NarayanaDataSourceWrapper.java diff --git a/pom.xml b/pom.xml index 83063f4b..3e6e1b04 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,7 @@ - com.atomikos - transactions-jdbc - 5.0.9 + org.jboss.narayana.jta + narayana-jta + 5.13.1.Final test @@ -171,10 +173,6 @@ derby.stream.error.file ${project.build.directory}/derby.log - - com.atomikos.icatch.log_base_dir - ${project.build.directory} - diff --git a/src/test/java/org/mybatis/cdi/JtaDatasourceFactory.java b/src/test/java/org/mybatis/cdi/JtaDatasourceFactory.java index c19df67e..e58b2ec1 100644 --- a/src/test/java/org/mybatis/cdi/JtaDatasourceFactory.java +++ b/src/test/java/org/mybatis/cdi/JtaDatasourceFactory.java @@ -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. @@ -15,36 +15,32 @@ */ 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(); + this.embeddedXADataSource = new EmbeddedXADataSource(); + this.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 this.dataSource; } @Override - public DataSource getDataSource() { - return this.ds; + public void setProperties(final Properties properties) { + this.embeddedXADataSource.setCreateDatabase(properties.getProperty("createDatabase")); + this.embeddedXADataSource.setDatabaseName(properties.getProperty("databaseName")); + this.embeddedXADataSource.setDataSourceName(properties.getProperty("resourceName")); } } diff --git a/src/test/java/org/mybatis/cdi/NarayanaDataSourceWrapper.java b/src/test/java/org/mybatis/cdi/NarayanaDataSourceWrapper.java new file mode 100644 index 00000000..e68bdb51 --- /dev/null +++ b/src/test/java/org/mybatis/cdi/NarayanaDataSourceWrapper.java @@ -0,0 +1,92 @@ +/* + * 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.sql.DataSource; +import javax.sql.XADataSource; + +public class NarayanaDataSourceWrapper implements DataSource { + private final XADataSource xaDataSource; + + public NarayanaDataSourceWrapper(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 unwrap(final Class iface) throws SQLException { + if (isWrapperFor(iface)) { + return (T) this; + } + throw new SQLException(getClass() + " is not a wrapper for " + iface); + } +} diff --git a/src/test/java/org/mybatis/cdi/UserTransactionProvider.java b/src/test/java/org/mybatis/cdi/UserTransactionProvider.java index 5dfe1bca..7167ff22 100644 --- a/src/test/java/org/mybatis/cdi/UserTransactionProvider.java +++ b/src/test/java/org/mybatis/cdi/UserTransactionProvider.java @@ -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. @@ -15,8 +15,6 @@ */ package org.mybatis.cdi; -import com.atomikos.icatch.jta.UserTransactionManager; - import javax.enterprise.context.ApplicationScoped; import javax.enterprise.inject.Produces; import javax.transaction.UserTransaction; @@ -26,7 +24,7 @@ public class UserTransactionProvider { @Produces @ApplicationScoped public UserTransaction initTX() { - return new UserTransactionManager(); + return com.arjuna.ats.jta.UserTransaction.userTransaction(); } } diff --git a/src/test/resources/org/mybatis/cdi/CreateDB_JTA.sql b/src/test/resources/org/mybatis/cdi/CreateDB_JTA.sql index 7cbb0a9d..fc2ff2b7 100644 --- a/src/test/resources/org/mybatis/cdi/CreateDB_JTA.sql +++ b/src/test/resources/org/mybatis/cdi/CreateDB_JTA.sql @@ -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. @@ -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'); diff --git a/src/test/resources/org/mybatis/cdi/mybatis-config_jta.xml b/src/test/resources/org/mybatis/cdi/mybatis-config_jta.xml index 0ae0bb02..a7645a8d 100644 --- a/src/test/resources/org/mybatis/cdi/mybatis-config_jta.xml +++ b/src/test/resources/org/mybatis/cdi/mybatis-config_jta.xml @@ -1,7 +1,7 @@