import java.sql.*;

public class LocLeak {

    public static int NUMROWS  = 100000;

    public static void main(String args[]) throws Exception {

	Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
	Connection conn = DriverManager.getConnection("jdbc:derby:wombat;create=true");
	conn.setAutoCommit(false);
	load_db(conn);
        dump_db(conn);
	conn.close();
	
    }

    public static void load_db(Connection conn) throws SQLException
    {
	Statement s = conn.createStatement();
	try {
	    s.executeUpdate("DROP TABLE LOB_TAB");
	} catch (SQLException se) {
	    //ignore
	}
	s.executeUpdate("CREATE TABLE LOB_TAB (ID INT, VAL CLOB)");
	s.close();
	PreparedStatement ps = conn.prepareStatement("INSERT INTO LOB_TAB VALUES(?,?)");
	for (int i = 0 ; i < NUMROWS;i++)
	{
	    ps.setInt(1,i);
	    ps.setString(2,"" + i);
	    ps.executeUpdate();
	    if (i % 1000 == 0) {
		System.out.println("inserting row " + i);
		conn.commit();
	    }
	}
	conn.commit();
	ps.close();
    }


    public static void dump_db(Connection conn) throws SQLException {
	Statement s = conn.createStatement();
	ResultSet rs = s.executeQuery("SELECT * from LOB_TAB");
	while (rs.next()) {
	    int r = rs.getInt(1);
	    Clob c = rs.getClob(2);
	    String val = c.getSubString(1,100);
	    if (r % 1000 == 0)
		System.out.println("Retrieving row " + r);
	    c.free();
	}
	rs.close();
	s.close();
    }


}
