Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ikasan 2370 add service to manage indoubt transactions v3 #1299

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
22 changes: 17 additions & 5 deletions ikasaneip/persistence/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<scope>provided</scope>
</dependency>

<!-- required for unit tests -->
<dependency>
<groupId>org.hamcrest</groupId>
Expand All @@ -105,15 +111,21 @@
</dependency>

<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>provided</scope>
<groupId>org.ikasan</groupId>
<artifactId>ikasan-transaction-arjuna</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.ikasan</groupId>
<artifactId>ikasan-spring-resource</artifactId>
<scope>test</scope>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.ikasan.persistence;

import org.ikasan.persistence.dao.HibernateInDoubtTransactionDaoImpl;
import org.ikasan.persistence.service.InDoubtTransactionServiceImpl;
import org.ikasan.spec.persistence.dao.InDoubtTransactionDao;
import org.ikasan.spec.persistence.service.InDoubtTransactionService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@ImportResource("/persistence-transaction.xml")
public class InDoubtTransactionAutoConfiguration {

@Bean
InDoubtTransactionService inDoubtTransactionService(InDoubtTransactionDao inDoubtTransactionDao) {
return new InDoubtTransactionServiceImpl(inDoubtTransactionDao);
}
@Bean
InDoubtTransactionDao inDoubtTransactionDao() {
return new HibernateInDoubtTransactionDaoImpl();
}

@Bean
public LocalContainerEntityManagerFactoryBean persistenceEntityManager(@Qualifier("ikasan.ds")DataSource dataSource
, JpaVendorAdapter jpaVendorAdapter, @Qualifier("platformJpaProperties") Properties platformJpaProperties) {
LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean
= new LocalContainerEntityManagerFactoryBean();
localContainerEntityManagerFactoryBean.setDataSource(dataSource);
localContainerEntityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
localContainerEntityManagerFactoryBean.setJpaProperties(platformJpaProperties);
localContainerEntityManagerFactoryBean.setPersistenceUnitName("persistence");
localContainerEntityManagerFactoryBean.setPersistenceXmlLocation("classpath:persistence-persistence.xml");

return localContainerEntityManagerFactoryBean;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.ikasan.persistence.dao;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import org.ikasan.persistence.model.InDoubtTransactionImpl;
import org.ikasan.spec.persistence.dao.InDoubtTransactionDao;
import org.ikasan.spec.persistence.model.InDoubtTransaction;

import java.util.List;

public class HibernateInDoubtTransactionDaoImpl implements InDoubtTransactionDao {

@PersistenceContext(unitName = "persistence")
private EntityManager entityManager;

@Override
public List<InDoubtTransaction> getInDoubtTransactions() {
return entityManager.createNativeQuery(
"SELECT * FROM INFORMATION_SCHEMA.IN_DOUBT", InDoubtTransactionImpl.class)
.getResultList();
}

@Override
public InDoubtTransaction getInDoubtTransaction(String transactionName) {
List<InDoubtTransaction> inDoubtTransactions = this.getInDoubtTransactions();

for (InDoubtTransaction inDoubtTransaction: inDoubtTransactions) {
if(inDoubtTransaction.getTransactionName().equalsIgnoreCase(transactionName)) {
return inDoubtTransaction;
}
}

return null;
}

@Override
public void commitInDoubtTransaction(String transactionName) {
if(this.getInDoubtTransaction(transactionName) == null) {
throw new RuntimeException(String.format("An in doubt transaction with name[%s] does not exist in the database!" +
" Unable to commit the in doubt transaction!", transactionName));
}
entityManager.createNativeQuery("COMMIT TRANSACTION " + transactionName).executeUpdate();
}

@Override
public void rollbackInDoubtTransaction(String transactionName) {
if(this.getInDoubtTransaction(transactionName) == null) {
throw new RuntimeException(String.format("An in doubt transaction with name[%s] does not exist in the database!" +
" Unable to rollback the in doubt transaction!", transactionName));
}
entityManager.createNativeQuery("ROLLBACK TRANSACTION " + transactionName).executeUpdate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.ikasan.persistence.model;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.ikasan.spec.persistence.model.InDoubtTransaction;

import java.util.Objects;

@Entity
@Table(name="IN_DOUBT")
public class InDoubtTransactionImpl implements InDoubtTransaction {

@Id
@Column(name="TRANSACTION_NAME")
private String transactionName;
@Column(name="TRANSACTION_STATE")
private String transactionState;

@Override
public String getTransactionName() {
return transactionName;
}

@Override
public void setTransactionName(String transactionName) {
this.transactionName = transactionName;
}

@Override
public String getTransactionState() {
return transactionState;
}

@Override
public void setTransactionState(String transactionState) {
this.transactionState = transactionState;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof InDoubtTransactionImpl that)) return false;
return Objects.equals(transactionName, that.transactionName)
&& Objects.equals(transactionState, that.transactionState);
}

@Override
public int hashCode() {
return Objects.hash(transactionName, transactionState);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.ikasan.persistence.service;

import org.ikasan.spec.persistence.dao.InDoubtTransactionDao;
import org.ikasan.spec.persistence.model.InDoubtTransaction;
import org.ikasan.spec.persistence.service.InDoubtTransactionService;

import java.util.List;


/**
* The InDoubtTransactionServiceImpl class is an implementation of the InDoubtTransactionService interface.
* It provides methods for retrieving, committing, and rolling back in-doubt transactions.
*/
public class InDoubtTransactionServiceImpl implements InDoubtTransactionService {

private InDoubtTransactionDao inDoubtTransactionDao;

/**
* Constructor.
*
* InDoubtTransactionServiceImpl class is an implementation of the InDoubtTransactionService interface.
*
* @param inDoubtTransactionDao the implementation of InDoubtTransactionDao used for data access
*/
public InDoubtTransactionServiceImpl(InDoubtTransactionDao inDoubtTransactionDao) {
this.inDoubtTransactionDao = inDoubtTransactionDao;
if(this.inDoubtTransactionDao == null) {
throw new IllegalArgumentException("inDoubtTransactionDao cannot be null!!");
}
}

@Override
public List<InDoubtTransaction> getInDoubtTransactions() {
return this.inDoubtTransactionDao.getInDoubtTransactions();
}

@Override
public InDoubtTransaction getInDoubtTransaction(String transactionName) {
return this.inDoubtTransactionDao.getInDoubtTransaction(transactionName);
}

@Override
public void commitInDoubtTransaction(String transactionName) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also add Service to CommitAll and RollBackAll?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrzej-majewski I have added feature to commit and roll back all.

this.inDoubtTransactionDao.commitInDoubtTransaction(transactionName);
}

@Override
public void rollbackInDoubtTransaction(String transactionName) {
this.inDoubtTransactionDao.rollbackInDoubtTransaction(transactionName);
}

@Override
public void commitAllInDoubtTransactions() {
this.inDoubtTransactionDao.getInDoubtTransactions()
.forEach(inDoubtTransaction -> this.commitInDoubtTransaction(inDoubtTransaction.getTransactionName()));
}

@Override
public void rollbackAllInDoubtTransactions() {
this.inDoubtTransactionDao.getInDoubtTransactions()
.forEach(inDoubtTransaction -> this.rollbackInDoubtTransaction(inDoubtTransaction.getTransactionName()));
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.ikasan.persistence.PersistenceAutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.ikasan.persistence.PersistenceAutoConfiguration\
,org.ikasan.persistence.InDoubtTransactionAutoConfiguration
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.ikasan.persistence.PersistenceAutoConfiguration
org.ikasan.persistence.PersistenceAutoConfiguration
org.ikasan.persistence.InDoubtTransactionAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="persistence" transaction-type="JTA">
<class>org.ikasan.persistence.model.InDoubtTransactionImpl</class>
</persistence-unit>
</persistence>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
# //
# //
# // $Id$
# // $URL$
# //
# // ====================================================================
# // Ikasan Enterprise Integration Platform
# //
# // Distributed under the Modified BSD License.
# // Copyright notice: The copyright for this software and a full listing
# // of individual contributors are as shown in the packaged copyright.txt
# // file.
# //
# // All rights reserved.
# //
# // Redistribution and use in source and binary forms, with or without
# // modification, are permitted provided that the following conditions are met:
# //
# // - Redistributions of source code must retain the above copyright notice,
# // this list of conditions and the following disclaimer.
# //
# // - Redistributions in binary form must reproduce the above copyright notice,
# // this list of conditions and the following disclaimer in the documentation
# // and/or other materials provided with the distribution.
# //
# // - Neither the name of the ORGANIZATION nor the names of its contributors may
# // be used to endorse or promote products derived from this software without
# // specific prior written permission.
# //
# // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# // ====================================================================
# //
# // Author: Ikasan Development Team
# //
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<aop:config>
<aop:pointcut id="persistenceDaoInvocationCommit"
expression="execution(* org.ikasan.spec.persistence.dao.InDoubtTransactionDao.commitInDoubtTransaction(..))"/>
<aop:pointcut id="persistenceDaoInvocationRollback"
expression="execution(* org.ikasan.spec.persistence.dao.InDoubtTransactionDao.rollbackInDoubtTransaction(..))"/>

<aop:advisor advice-ref="requiresNew-TransactionAdvice" pointcut-ref="persistenceDaoInvocationCommit"/>
<aop:advisor advice-ref="requiresNew-TransactionAdvice" pointcut-ref="persistenceDaoInvocationRollback"/>
</aop:config>


</beans>
Loading