Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

Why does Google Cloud SQL (using JDBC) take longer to insert records from Google App Engine than from my personal computer?

I can insert 2000 records in 3.5 seconds from my computer, GAE take 14.0 seconds to do the same thing. I cannot see how I can get the Google Driver to use the rewriteBatchedStatements setting.

Here is my relevant Java code:

package year2016.tax8949.database2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;

public class Form89492016TableInserter {

private static final Logger log 
    = Logger.getLogger( "Form89492016TableInserter" );

protected static final String USERNAME = "xxxx";

protected static final String PASSWORD = "xxxxx";

protected static final String INSTANCE_CONNECTION_NAME 
    = "xxxx:us-central1:xxxxx";

protected static final String DB_NAME = "xxxxxx";

protected static final String IP_ADDRESS = "xxx.1xx.2xx.4x";

protected static final String SQL_INSERT 
    = "INSERT into Forms1099B ( orderNumber,acctId,qty,secDesc,dateAcq,dateSold,salesPrice,cost,basisAdj,washAdj,nomineeAdj,otherAdj,term,basisRep,rep1099B,tranType,dateAcqVar,covered,symbol,expired ) VALUES ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? )";


// Whether connecting from Google App Engine
private boolean fromGAE = false;

// JDBC connection
private Connection conn = null;

public Form89492016TableInserter( boolean fromGAE ) {
    this.fromGAE = fromGAE;
}

public void insertBatch( List<Forms1099BRecordBean> records ) {

    int batchSize = 500;

    insertBatchUsingSize( records, batchSize );
}

public void insertBatchUsingSize( List<Forms1099BRecordBean> records, 
                                  int batchSize ) {

    try {

        initializeConnection( );

        doInsertions( records, batchSize );

        closeConnection( );

    }
    catch( SQLException e ) {
        log.severe( e.getMessage( ) );
    }

}

public void closeConnection( ) throws SQLException {

    if ( conn != null ) { 
        conn.close( ); 
    } 

}

public void initializeConnection( ) throws SQLException {

    String driverName 
        = ( fromGAE ) ? "com.mysql.jdbc.GoogleDriver" : 
                        "com.mysql.jdbc.Driver";

    try {
        Class.forName( driverName );
    }
    catch( ClassNotFoundException e ) {
        log.severe( e.getMessage( ) );
        return;
    }

    if ( fromGAE ) {

        String connectionString 
            = String.format( 
                 "jdbc:google:mysql://%s/%s?user=root&password=%s", 
                 INSTANCE_CONNECTION_NAME, 
                 DB_NAME, 
                 PASSWORD );

        conn = DriverManager.getConnection( connectionString );

    }
    else {

        String url = String.format( "jdbc:mysql://%s:3306/%s",
                                    IP_ADDRESS,
                                    DB_NAME );

        Properties props = new Properties();
        props.setProperty( "user", USERNAME );
        props.setProperty( "password", PASSWORD );
        props.setProperty( "rewriteBatchedStatements", "true" );

        conn = DriverManager.getConnection( url, props );    

    }

}

private void doInsertions( List<Forms1099BRecordBean> records,
                           int batchSize ) throws SQLException {

    try ( PreparedStatement stmt = conn.prepareStatement( SQL_INSERT ) ) {

        for (int i = 0; i < records.size( ); i++) {

            Forms1099BRecordBean item = records.get( i );

            stmt.setString( 1, item.getOrderNumber() ); 
            stmt.setString( 2, item.getAcctId() ); 
            stmt.setString( 3, item.getQty() ); 
            stmt.setString( 4, item.getSecDesc() ); 
            stmt.setDate( 5, item.getDateAcq() ); 
            stmt.setDate( 6, item.getDateSold() ); 
            stmt.setBigDecimal( 7, item.getSalesPrice() ); 
            stmt.setBigDecimal( 8, item.getCost() ); 
            stmt.setBigDecimal( 9, item.getBasisAdj() ); 
            stmt.setBigDecimal( 10, item.getWashAdj() ); 
            stmt.setBigDecimal( 11, item.getNomineeAdj() ); 
            stmt.setBigDecimal( 12, item.getOtherAdj() ); 
            stmt.setString( 13, item.getTerm() ); 
            stmt.setString( 14, item.getBasisRep() ); 
            stmt.setString( 15, item.getRep1099B() ); 
            stmt.setString( 16, item.getTranType() ); 
            stmt.setString( 17, item.getDateAcqVar() ); 
            stmt.setString( 18, item.getCovered() ); 
            stmt.setString( 19, item.getSymbol() ); 
            stmt.setString( 20, item.getExpired() ); 

            stmt.addBatch( );

            // Execute every N items.
            if ( (i + 1) % batchSize == 0 ) {
                stmt.executeBatch( );     
            }

        }

        stmt.executeBatch( );

    }

}

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...