JDBC transaction savepoint handling

2010-03-31  来源:本站原创  分类:Java  人气:422 

In the JDBC things processed, can be applied to save the point of technology, the one thing in the processing of part of the submission.
The following example, three treatments

7, Zhang San by 10 yuan
9, John Doe by 10 yuan
8, Zhao 6 plus 10 yuan,

Wrong place at 8, then the processing of the 8 submission, using the save point technology.

Sample Code:

package com.test.tx;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;

import com.test.jdbc.DBUtil;

public class SavaPointTest {

        /**
         * @param args
         */
        public static void main(String[] args) {

                try {

                        test();
                } catch (SQLException e) {
                        e.printStackTrace();
                }
        }
        /**
         *  Save points are available from the Connection  .rollback  Method referenced in the current transaction.
         *  When the transaction is rolled back to a savepoint in the savepoint after all the changes you made will be undone.
         * @throws SQLException
         */
        static void test()throws SQLException{
                Connection conn=null;
                Statement st=null;
                ResultSet rs=null;
                Savepoint sp=null;
                try{
                        conn=DBUtil.getConnection();
                        // Initialization does not automatically commit
                        DBUtil.setAutoCommit(conn, false);

                        //  John minus $ 10
                        st=conn.createStatement();
                        String sql="update user set money=money-10 where";
                        st.executeUpdate(sql);
                        sp=conn.setSavepoint();// Set the save point  

                        //  Paul Lee plus $ 10
                        sql="update user set money=money-10 where";
                        st.executeUpdate(sql);

                        sql="select money from user where";
                        rs=st.executeQuery(sql);

                        float Money=0.0f;
                        if(rs.next()){
                                Money=rs.getFloat("money");
                        }
                        //  Zhao six money more than 1000 Yuan (may)
                        if(Money>1000){
                                throw new RuntimeException(" You have exceeded the maximum!  ");
                        }
                        sql="update user set money=money+10 where";
                        st.executeUpdate(sql);

                        DBUtil.commit(conn);

                }catch(RuntimeException e){
                        if(sp!=null){
                                // Indicates that the submission of a transaction
                                conn.rollback(sp);
                                DBUtil.commit(conn);
                }
                        throw e;
                }catch(SQLException e)
                {
                        DBUtil.rollback(conn);
                        throw e;
                }
                finally{
                        DBUtil.close(rs);
                        DBUtil.close(st);
                        DBUtil.close(conn);
                }

        }
}

Implementation of the file, view the database records, we find that Zhang San declined by 10 yuan, but, Xiao-Ming Zhao reduced by 10 million and 6 nor an increase of 10 did not conduct the operation.

Summary:
1, the beginning not to make connection set to auto-commit
2, the middle set the save point
3, a rollback to save point where you want to use
4, finally do not forget to commit

相关文章
  • JDBC transaction savepoint handling 2010-03-31

    In the JDBC things processed, can be applied to save the point of technology, the one thing in the processing of part of the submission. The following example, three treatments 7, Zhang San by 10 yuan 9, John Doe by 10 yuan 8, Zhao 6 plus 10 yuan, Wr

  • JDBC Transaction Processing Exploration 2010-02-23

    Java, transaction processing Under normal circumstances, J2EE application server supports JDBC transaction, JTA (Java Transaction API) transaction, the container management. Under normal circumstances, it is best not to be used in the program of the

  • Management (3) - JDBC transaction management simple implementation 2010-08-24

    Overview: In order to study management of its affairs to achieve the following in a simple version of the transaction manager, to conduct experiments. Simplifies the management of various abnormal types of transaction management considerations. Trans

  • JDBC transaction (transfer) 2011-04-21

    Of: Jack Shirazi Development of applications through the ACID test Service allows developers to work much simpler. Through the JDBC API and the Oracle9i relational database, such as the use affairs function, multi-user application in the update, you

  • spring jdbc transaction management 2008-12-01

    Previously, when doing spring security, they noted that a detail that is when I only use spring with hibernate core functionality, the surprise must rely on the spring-jdbc package, at least report the following error: org / springframework / jdbc /

  • JDBC transaction isolation level 2010-07-10

    If the DBMS supports transaction processing, it must have some way to manage both the transaction and operation of a database of possible conflicts. Users can specify the transaction isolation level to specify the DBMS should spend much effort to res

  • JDBC transaction. JTA transaction 2010-09-23

    [Color = green] [/ color] [size = large] [/ size] [b]JDBC Transaction .JTA Affairs [/b] Under normal circumstances, J2EE application server supports JDBC transaction, JTA transaction, the container management. Discussed here the difference between JT

  • JTA transactions and general instances of JDBC transaction rollback 2010-12-17

    JDBC transaction rollback is given below the code sample: Java code public void processT(String orders) { Context initCtx = new InitialContext(); javax.sql.DataSource ds = javax.sql.DataSource)initCtx.lookup ("java:comp/env/jdbc/OrdersDB"); java

  • CSDN see the article about the Spring JDBC transaction management article (full-content comparison) 2011-07-08

    JDBC transaction management Spring provides programmatic transaction management (Programmatic transaction manage-ment) and declarative transaction management (Declarative transaction management), to provide different services to achieve a consistent

  • With spring + hibernate and jdbc transaction 2011-07-07

    http://www.blogjava.net/RongHao/archive/2007/10/09/151411.html With spring + hibernate and jdbc transaction Problem Background: We are a company workflow, customer purchasing our products, to embed their project. Our workflow uses a spring + hibernat

  • JavaBean way to use JDBC transaction transfer 2010-05-11

    In the JavaBean database operations, a transaction refers to one or more sql database update statements formed an indivisible unit of work. Only when the affairs of all the operations are properly completed, the transaction can be submitted to the da

  • The database JDBC result set handling of multi- 2010-06-10

    Under normal circumstances, statement will only return after the implementation of execute a single result set, if you want a return multiple result sets, you have to use the JDBC driver handling of multiple result sets. Various database vendors have

  • jdbc transaction processing and batch processing 2010-10-24

    [Size = large] [/ size] [/ b] [b] / / use of services import java.sql .*; public class Transaction { public static void main (String [] args) { Connection conn = null; Statement sta = null; boolean auto = true; try { conn = DatabaseConnection.getOrac

  • JDBC - date type handling 2011-09-16

    Of the table field type: DATE Different versions of the package when you use getObject OJDBC drive out of different types. ojdbc14_10_2_0_4.jar out is java.sql.Date ojdbc14.jar out the java.sql.Timestamp Connection conn = null; PreparedStatement ptmt

  • JDBC Affairs and the difference between JTA transaction 2010-05-12

    In that the difference between them before, first consider the following issues: 1, getCurrentSession () and openSession () differences? * Use getCurrentSession () to create the session will be bound to the current thread, while using openSession ()

  • JDBC事务管理及SavePoint示例 2014-02-07

    默认情况下,当我们创建一个数据库连接时,会运行在自动提交模式(Auto-commit)下.这意味着,任何时候我们执行一条SQL完成之后,事务都会自动提交.所以我们执行的每一条SQL都是一个事务,并且如果正在运行DML或者DDL语句,这些改变会在每一条SQL语句结束的时存入数据库.有时候我们想让一组SQL语句成为事务的一部分,那样我们就可以在所有语句运行成功的时候提交,并且如果出现任何异常,这些语句作为事务的一部分,我们可以选择将其全部回滚. 让我们通过一个简单的示例理解一下,这里使用JDBC的事

  • JDBC services and JTA (XA) transaction 2010-06-12

    Java code JDBC services and JTA (XA) transaction Service Description Under normal circumstances, J2EE application server supports JDBC transaction, JTA (Java Transaction API) transaction (typically managed by the container). Usually, it is best not t

  • jdbc jta transaction management and Summary 2010-07-28

    jdbc transaction management is limited to a single data source and can not span multiple data sources. Service is the default auto-commit, that is, to submit a sql. Here an example of two data sources: Management DAO Data Source transactionManager da

  • JAVA transaction, JTA, JDBC, JDO, DAO, JNDI concepts 2010-10-20

    JAVA Services I. What is Java Services Usually the idea that the only database-related matters. Transaction is subject to ISO / IEC developed by ACID principle. Is the atomic ACID (atomicity), consistency (consistency), isolation (isolation) and pers

  • Pure JDBC.Hibernate.Spring of AOP declarative transaction management Summary 2011-08-16

    Introduction: Courses at the center recently to learn the Spring Framework's declarative transaction management chapter, today hit a small example of the three I now know the management of affairs to make a simple comparison, the way to consolidate t