DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Using spwrap Inside Spring Transactions

Using spwrap Inside Spring Transactions

The spwrap has made progress in working with Spring, including being able to work with Spring transactions and better rollback support.

Mohammed Hewedy user avatar by
Mohammed Hewedy
·
Apr. 07, 17 · Tutorial
Like (0)
Save
Tweet
Share
4.40K Views

Join the DZone community and get the full member experience.

Join For Free

spwrap is a tiny framework that simplifies calls to database stored procedures in Java. Read this introduction for an idea of what spwrap is and does before continuing on with this post.

We have talked before about how to use spwrap in Spring Boot applications. Today, we will talk about a new feature just released in version 0.0.18 — spwrap can participate in Spring transactions.

spwrap itself doesn't allow spanning transactions across DAO method calls, but as part of 0.0.18, it will participate in Spring transactions if spwrap is used inside Spring and there's an active transaction.

Suppose we have a Spring project where a datasource transaction manager is enabled. And we have SupplierDAO, which is a spwrap DAO defined like this:

public interface SupplierDAO {

    @StoredProc("insert_new_supplier")
    void insertSupplier(@Param(VARCHAR) String name);
}


And we have a domain object supplier and its spring-data-jpa repository 

@Entity
public class Supplier {

    @Id    @GeneratedValue(strategy = AUTO)
    private Long id;

    @Column(unique = true)
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
// -- repository
public interface SupplierRepo extends JpaRepository<Supplier, Long> {
}


And here's the service class: 

@Service
public class SupplierService {

    private SupplierDAO supplierDAO;
    private SupplierRepo supplierRepo;

    public SupplierService(SupplierDAO supplierDAO, SupplierRepo supplierRepo) {
        this.supplierDAO = supplierDAO;
        this.supplierRepo = supplierRepo;
    }

    @Transactional
    public void add2Suppliers(){
        final String supplierName = "Abdullah";

        supplierDAO.insertSupplier(supplierName);   // << will rolled-back

        Supplier s2 = new Supplier();
        s2.setName(supplierName);
        supplierRepo.save(s2);      // throws exception
    }

    public List<Supplier> listSuppliers(){
        return supplierRepo.findAll();
    }
}


Now, because the supplierDAO.insertSupplier(supplierName) inserts a supplier with the name "Abdullah", and supplierRepo.save(s2) inserts a supplier with the same name, then the Spring Framework will throw a DataAccessException subclass and roll back the entire transaction.

Tha means the stored procedure that is executed as a result of calling supplierDAO.insertSupplier(supplierName) will be rolled back as well. Again, this is a new feature as part of spwrap 0.0.18.  

You can see the full example on GitHub.

Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Using JSON Web Encryption (JWE)
  • Apache Kafka vs. Memphis.dev
  • How To Validate Three Common Document Types in Python
  • Handling Automatic ID Generation in PostgreSQL With Node.js and Sequelize

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: