Re: outOfMemoryError

From: Luis Vilar Flores <lflores(at)evolute(dot)pt>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: outOfMemoryError
Date: 2006-08-29 22:02:34
Message-ID: 0D2330AE-008F-47DD-858F-491CB18652F1@evolute.pt
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

I wrote a program for moving full databases between different db
engines, the main loop for inserting is:

The main difference is that I use batch insert on prepared statements.

public class Move
{
public static final int MAX_BATCH_ROWS = 4096;

public static move( Connection CON_SRC, Connection CON_DEST, String
table )
throws Exception
{
Statement stm = CON_SRC.createStatement( ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY );
ResultSet rs = statement.executeQuery( "SELECT * FROM " + table );
StringBuffer buff = new StringBuffer( "INSERT INTO " );
StringBuffer args = new StringBuffer();
buff.append( table );
buff.append( " ( " );
ResultSetMetaData rsmd = rs.getMetaData();
for( int j = 1; j <= rsmd.getColumnCount(); ++j )
{
if( j != 1 )
{
buff.append( ", " );
args.append( ", " );
}
buff.append( rsmd.getColumnName( j ) );
args.append( "?" );
}
buff.append( " ) VALUES ( " );
buff.append( args );
buff.append( " )" );
String insert = buff.toString();
PreparedStatement pstm = CON_DEST.prepareStatement( insert );
System.out.println( "I: " + i + " " + TABLES[ i ] );
int typesCache[] = new int[ rsmd.getColumnCount() + 1 ];
for( int j = 1; j <= rsmd.getColumnCount(); ++j )
{
typesCache[ j ] = rsmd.getColumnType( j );
}

int rows = 0;
while( rs.next() )
{
for( int j = 1; j < typesCache.length; ++j )
{
Object data = rs.getObject( j );
pstm.setObject( j, rs.getObject( j ), typesCache[ j ] );
}
++rows;
if( ( rows % MAX_BATCH_ROWS ) == 0 )
{
pstm.executeBatch();
}
}
rs.close();
pstm.close();
}

}

Please have attention, this code was copy/paste from 2 classes (my
program is multi-threaded for performance - async read/write and
multiple tables at once)
This method works on at least 5 million rows.

Luis Flores

Analista de Sistemas

Evolute - Consultoria Informática

Email: lflores(at)evolute(dot)pt

Tel: (+351) 212949689

AVISO DE CONFIDENCIALIDADE
Esta mensagem de correio electrónico e eventuais ficheiros anexos são
confidenciais e destinados apenas à(s) pessoa(s) ou entidade(s) acima
referida(s), podendo conter informação privilegiada e confidencial, a
qual não poderá ser divulgada, copiada, gravada ou distribuída nos
termos da lei vigente. Caso não seja o destinatário da mensagem, ou
se ela lhe foi enviada por engano, agradecemos que não faça uso ou
divulgação da mesma. A distribuição ou utilização da informação nela
contida é interdita. Se recebeu esta mensagem por engano, por favor
notifique o remetente e apague este e-mail do seu sistema. Obrigado.

CONFIDENTIALITY NOTICE
This e-mail transmission and eventual attached files are intended
only for the use of the individual(s) or entity(ies) named above and
may contain information that is both privileged and confidential and
is exempt from disclosure under applicable law. If you are not the
intended recipient, you are hereby notified that any disclosure,
copying, distribution or use of any of the information contained in
this transmission is strictly restricted. If by any means you have
received this transmission in error, please immediately notify the
sender and delete this e-mail from your system. Thank you.

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message Kris Jurka 2006-08-30 00:47:13 Re: outOfMemoryError
Previous Message Bruce Momjian 2006-08-29 20:10:12 Re: [HACKERS] [PATCHES] log_statement output for protocol