Learn about transaction savepoints (with Java)

What you can see in this article

--There is a mechanism called a transaction savepoint. --Sample code for use with Java

About the mechanism of transaction savepoints

In the first place, a transaction refers to "a series of processing flow for performing a certain processing". If any of the processes is completed in a series of processes, it is often the case that the processes updated up to that point are rewound and returned to the state before the processes were performed. However, depending on the situation, you may want to return to a certain point where you do not want to return everything. In such a case, you can set a Savepoint and return (roll back) to that point.

Sample code for use with Java

Describe the sample code assuming the process of registering the argument name in the list table.

point

--Get Savepoint between getting Connection and getting PreparedStatement. --Check if the DB does not support Savepoint, and then get the Savepoint object. --When rolling back to the point where you got the Savepoint, pass a Savepoint object like rollback (save). --For DBs that do not support Savepoint, the Savepoint object remains null, so check it when rolling back and if it is null, do a normal rollback.

Supplement

--The Savepoint class is included in the java.sql package. --It is possible to have multiple Savepoints in one Connection. In that case, you can give each Savepoint a name. (Not implemented in this sample code)

Java


public void regist(String name) throws SQLException{
    Connection con = null;
    Savepoint save = null;
    String sql = null;
    PreparedStatement ps = null;
    
    try{
        con = this.getConnection();
        if(con.getMetaData().supportsSavepoints()){
            save = con.setSavepoint();
        }
        sql = "insert into list (name) values (?)";
        ps = con.prepareStatement(sql);
        ps.setString(1, name);
        ps.executeUpdate();
        ps.close()
    }catch(SQLException e){
        if(save != null){
            con.rollback(save);
        }
        else{
            con.rollback();
        }
    }finally{
        if(con!=null){
            try{
                con.close();
            }catch(SQLException e){
            }
        }
    }
}

Recommended Posts

Learn about transaction savepoints (with Java)
Java to learn with ramen [Part 1]
About signature authentication with java 1st
Learn Java with "So What" [For beginners]
About Java interface
[Java] About Java 12 features
[Java] About arrays
Something about java
Where about java
About Java features
About Java threads
[Java] About interface
About Java class
About Java arrays
About java inheritance
About interface, java interface
About List [Java]
About java var
About Java literals
About Java commands
About Java log output
About Java functional interface
Install java with Homebrew
Java, about 2D arrays
About class division (Java)
About Java StringBuilder class
Change seats with java
Install Java with Ansible
[Java] About Singleton Class
About Java method binding
Comfortable download with JAVA
[Java] About anonymous classes
About method splitting (Java)
[Java Silver] About initialization
Switch java with direnv
About Java Array List
About Java Polymorphism super ()
About inheritance (Java Silver)
About Java String class
About Java access modifiers
Download Java with Ansible
About Java lambda expressions
About Java entry points
About Java 10 Docker support
Personal summary about Java
Let's scrape with Java! !!
[Java] About enum type
A story about developing ROS called rosjava with java
All about Java programming
Build Java with Wercker
About java abstract class
Endian conversion with JAVA
Learn about the spec while shortening FizzBuzz written in Java
About the behavior when doing a file map with java
Java multi-project creation with Gradle
A note about Java GC
Getting Started with Java Collection
About an instance of java
Basic Authentication with Java 11 HttpClient
Let's experiment with Java inlining
Run batch with docker-compose with Java batch