jdbc intolerably slow issue, with code example
Posted in 2003
I'm using jdbc to connect to a database on my local area network. With the java program below, which loads a 27 megabyte chunk of data into a Byte column, the program takes 733 seconds. Ftp can transfer the data from client to server machine in 20 seconds. Can anybody suggest why it takes so long? Eric. import java.util.*; import com.informix.jdbc.*; import java.sql.*; import java.net.*; /** * * @author Eric */ public class TestLargeLoad { Connection connection; private final int maxVarCharSIZE = 255; /** Creates a new instance of LoadNetCDF */ public TestLargeLoad(Connection connection) throws SQLException { this.connection = connection; } private void defineTable( String tablename) throws SQLException { // // try to drop the table if it already exists // try { CallableStatement cs = connection.prepareCall("drop table " + tablename + ";" ); cs.execute(); } catch( SQLException e1 ) { System.out.println(e1.toString()); } StringBuffer insertText = new StringBuffer(256); insertText.append("create table "); insertText.append( tablename); insertText.append(" (a byte);"); Statement ast = connection.createStatement(); ast.execute(insertText.toString()); ast.close(); } public class FakeStream extends java.io.InputStream { /** Reads the next byte of data from the input stream. The value byte is * returned as an <code>int</code> in the range <code>0</code> to * <code>255</code>. If no byte is available because the end of the stream * has been reached, the value <code>-1</code> is returned. This method * blocks until input data is available, the end of the stream is detected, * or an exception is thrown. * * <p> A subclass must provide an implementation of this method. * * @return the next byte of data, or <code>-1</code> if the end of the * stream is reached. * @exception IOException if an I/O error occurs. * */ int pos = 0; final int size = 27*1024*1024; public int read() { if( pos >= size ) { System.out.println("read last byte"); return -1; } else { pos++; return 27; } } public int getLength() { return size; } public int available() { return size - pos; } } private void loadTable(String tableName ) throws SQLException, java.io.IOException { // // build a count of the number of arguments to be inserted. // StringBuffer insertText = new StringBuffer(256); insertText.append("insert into " + tableName + " values(?);"); // // insert first the geo grids, then any nongeo grids. and attributes. // PreparedStatement pstmt = connection.prepareStatement(insertText.toString()); int paramNo = 1; FakeStream astream = new FakeStream(); pstmt.setBinaryStream(paramNo, astream, astream.getLength()); System.gc(); System.out.println("about to execute"); long startTime = System.currentTimeMillis(); pstmt.executeUpdate(); System.out.println("finished execute in"+ (System.currentTimeMillis()-startTime)); pstmt.close(); } private void load(String tablename, boolean doDefine, boolean doLoad) throws SQLException, java.io.IOException{ System.gc(); // force garbage collection so we don't break down later if( doDefine ) { defineTable(tablename); } if( doLoad ) { loadTable(tablename); } } public void close() throws java.sql.SQLException { if( connection != null ) { connection.close(); connection = null; } } public static void main( String args[] ) { String hostName = "yourHost"; String userId = "yourId"; String password = "yourPassWord"; String databaseName = "yourDataBaseName"; int portNumber = 1533; // port number for the database instance. String serverName = "yourServiceName"; System.out.println("free mem= " + Runtime.getRuntime().maxMemory()/(1024*1024)); for(int i = 0; i < args.length; i++ ) { System.out.println("saw arg " + args[i]); } try { String serverUrl = "jdbc:informix-sqli://" + hostName + ":" + portNumber + "/" + databaseName + ":informixserver=" + serverName + ";" + "lobcache=-1;" + "user=" + userId + ";" + "password=" + password + ";" ; Driver IfmxDrv = new IfxDriver(); Connection con = DriverManager.getConnection(serverUrl); TestLargeLoad t = new TestLargeLoad(con); t.load("ericfu", true, true); t.close(); } catch( MalformedURLException e2 ) { System.out.println("bad url: " + e2); } catch( SQLException e3 ) { System.out.println("saw " + e3); e3.printStackTrace(); } catch( java.io.IOException e4 ) { System.out.println("io exception " + e4); } } } ********************************************** Eric Davies, M.Sc. Barrodale Computing Services Ltd. Tel: (250) 472-4372 Fax: (250) 472-4373 Web: http://www.barrodale.com Email: eric@barrodale.com ********************************************** Mailing Address: P.O. Box 3075 STN CSC Victoria BC Canada V8W 3W2 Shipping Address: Hut R, McKenzie Avenue University of Victoria Victoria BC Canada V8W 3W2 **********************************************