Transaction management in ORACLE

Keywords: PHP Oracle SQL JDBC MySQL

Because of the work requirements, a large amount of data needs to be inserted into ORACLE data, and then the manual management of transaction opening is selected to insert a large amount of data. However, a pit is encountered here. Before the transaction management of mysql was opened to insert data, copying mysql is a bit impractical. Only 299 pieces of data can be inserted each time. At first, I thought there was a problem with the code. , check the code carefully and find out that there is no problem. Find the reason from the Internet. Use the following methods to enable ORACLE transaction management in JDBC and insert data in batches. Pro test available:

//towards***Insert organization data
    public void insertZZ(JSONArray ja) throws SQLException {
        //Connect intermediate Library
        String user = "****";
        String password = "*****";
        String url = "jdbc:oracle:thin:@ip:port/SID";
        String driver = "oracle.jdbc.driver.OracleDriver";
        Connection con = null; //A package TCP Long connected database long connected object
//        Statement stmt = null; //A package and management SQL Sentence java object

        PreparedStatement preparedStatement = null;

        //insert data
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url, user, password);

            //Set the transaction mode to manually commit the transaction:
//            con.setAutoCommit(false);
            //Sets the isolation level for the transaction.
//            con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

            //Run insert
            int j = 2;
            String sql = "INSERT INTO IUFO_UNIT_INFO(DR,LEVEL_CODE,TS,UNIT_CODE,UNIT_ID,UNIT_NAME,UNIT_TYPE) \n" +
                    "values(?,?,?,?,?,?,?)";
            preparedStatement = con.prepareStatement(sql);
            for (int i = 0; i < ja.size(); i++) {
                JSONObject jo = ja.getJSONObject(i);
                String dr = (String) jo.get("dr");
                String level_code = (String) jo.get("level_code");
                String ts = (String) jo.get("ts");
                String unit_code = (String) jo.get("code");
                String unit_id = (String) jo.get("unit_id");
                String unit_name = (String) jo.get("name");
                String unit_type = (String) jo.get("unit_type");

//                String sql = "INSERT INTO IUFO_UNIT_INFO(DR,LEVEL_CODE,TS,UNIT_CODE,UNIT_ID,UNIT_NAME,UNIT_TYPE) \n" +
//                        "values('"+dr+"','"+level_code+"','"+ts+"','"+unit_code+"','"+unit_id+"','"+unit_name+"','"+unit_type+"')";

                preparedStatement.setString(1,dr);
                preparedStatement.setString(2,level_code);
                preparedStatement.setString(3,ts);
                preparedStatement.setString(4,unit_code);
                preparedStatement.setString(5,unit_id);
                preparedStatement.setString(6,unit_name);
                preparedStatement.setString(7,unit_type);
                preparedStatement.addBatch();

//                int resultSet = preparedStatement.executeUpdate();
//                System.out.println(j);

            }
            preparedStatement.executeBatch();

            //Submission of affairs
            con.commit();


        } catch (Exception e) {
            // If the transaction is abnormal, roll back the transaction
            con.rollback();
        }finally {
      //Turn on transaction auto commit con.setAutoCommit(
true); try { if (preparedStatement != null) { preparedStatement.close(); } if (con != null) { con.close(); } } catch (SQLException e) { e.printStackTrace(); } } }

Posted by philvia on Wed, 16 Oct 2019 14:35:28 -0700