Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 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 @@ -95,10 +95,12 @@
<version>5.9.2</version>
<scope>test</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</artifactId>
<version>5.13.1.Final</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -171,10 +173,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
28 changes: 12 additions & 16 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,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"));
}

}
92 changes: 92 additions & 0 deletions src/test/java/org/mybatis/cdi/NarayanaDataSourceWrapper.java
Original file line number Diff line number Diff line change
@@ -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> T unwrap(final Class<T> iface) throws SQLException {
if (isWrapperFor(iface)) {
return (T) this;
}
throw new SQLException(getClass() + " is not a wrapper for " + iface);
}
}
6 changes: 2 additions & 4 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 javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.transaction.UserTransaction;
Expand All @@ -26,7 +24,7 @@ public class UserTransactionProvider {
@Produces
@ApplicationScoped
public UserTransaction initTX() {
return 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