简体   繁体   中英

java- using Commit Transaction to insert to table

I want to start a transaction in my java program to insert into my table in MYSQL.

My code:

 public void index(String path) throws Exception {   

        PDDocument document = PDDocument.load(new File(path));

            if (!document.isEncrypted()) {

                PDFTextStripper tStripper = new PDFTextStripper();
                String pdfFileInText = tStripper.getText(document);
                String lines[] = pdfFileInText.split("\\r?\\n");
                for (String line : lines) {
                    String[] words = line.split(" ");


                    String sql="insert IGNORE into  test.indextable values (?,?);";

                       preparedStatement = con.connect().prepareStatement(sql);
                     int i=0;
                    for (String word : words) {

                        // check if one or more special characters at end of string then remove OR
                        // check special characters in beginning of the string then remove
                    // insert every word directly to table db
                      word=word.replaceAll("([\\W]+$)|(^[\\W]+)", "");
                        preparedStatement.setString(1, path);
                        preparedStatement.setString(2, word);

                        preparedStatement.executeUpdate();
        System.out.print("Add ");



                }


            }

        con.connect().commit();
        preparedStatement.close();
     con.connect().close();

        System.out.println("Successfully commited changes to the database!");

I set AutoCommit to false in my connection class. However when I run the program, it usually prints out the message " Add" a couple of times before it stops there. If I keep waiting it eventually says: Lock Time Exceeding,please restart transaction .

EDIT:

Connection method(in a different class):

public Connection connect() throws Exception {
    Properties props=new Properties();


    InputStream in = getClass().getResourceAsStream("/db.properties");

    props.load(in);
    in.close();



    String driver = props.getProperty("jdbc.driver");
    if(driver!=null){
        Class.forName(driver);

    }

    String url=props.getProperty("jdbc.url");
    String username=props.getProperty("jdbc.username");
    String password=props.getProperty("jdbc.password");

    Connection con = DriverManager.getConnection(url,username,password);
    //con.setAutoCommit(false);

    return con;

}

Rollback function:

 public void rollbackEntries() throws Exception {

       con.connect().rollback();
       System.out.println("Successfully rolled back changes from the database!");


    }

You should use same connection for commit

 Connection con =con.connect();
 ....
 con.commit();
 con.close();

As aside notes, move to connection pool and catch exceptions to release resources also in case of an error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM