Re: jdbc and kill -14

  • From: "Giovanni Cuccu" <giovanni.cuccu@xxxxxxxxx>
  • To: ryan_gaffuri@xxxxxxxxxxx
  • Date: Wed, 29 Mar 2006 13:47:11 +0200

Hi ryan,
    sadly this is the documented behaviour (see par 3.18 of jdbc
driver manual for 10g2).
I see two possible solutions (I did partially test only the first)
1)Add a shutdown hook to your jvm and test if it is called when the
signal is caught
2)Add a signal handler; I search google for java sigalrm and found
some docs explaining ho to catch unix signals in Java I don't know how
recent the articles were but they all based their code on some sun.*
classes.
I create a simple test case composed of two classes which shows how to
installa shutdown hook that rollbacks the connection dml before
closing the jvm. you can adapt it to see if the hook is triggered at
the SIGALRM receiving.
It' s not production code but it may give you an idea.
Hope it helps,
    Giovanni


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
/**
 *
 * @author Giovanni Cuccu
 * @version $Revision$ - $Date$
 */
public class TestAutoCommit  {
    public TestAutoCommit() throws Exception {
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        conn = DriverManager.getConnection(
            "jdbc:oracle:thin:@linux:1521:orcl","test","test");
    }

    public Connection conn;

    public void test() throws Exception {

        try {
            PreparedStatement stmt = conn.prepareStatement("insert
into A ( X ) " +
                    "values (?)");
            conn.setAutoCommit(false);
            try {
                for (int i=0;i<100;i++) {
                    stmt.setString(1,Integer.toHexString(i));
                    stmt.execute();
                    if (i==90) {
                        throw new RuntimeException("AARGH");
                    }
                }
                conn.commit();
            } finally {
                stmt.close();
            }
        } finally {
            //conn.close();
        }

    }

    public static void main(String[] args) throws Exception {
        TestAutoCommit testAutoCommit = new TestAutoCommit();
        ShutdownHook hook = new ShutdownHook(testAutoCommit.conn);
        Runtime.getRuntime().addShutdownHook(hook);
        testAutoCommit.test();

    }

}

import java.sql.Connection;

public class ShutdownHook extends Thread {
    Connection conn;
    public ShutdownHook(Connection conn) {
        this.conn=conn;
    }
    public void run() {
        try {
            if (conn!=null) {
                conn.rollback();
                conn.close();
            }
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}



On 3/28/06, ryan_gaffuri@xxxxxxxxxxx <ryan_gaffuri@xxxxxxxxxxx> wrote:
>
>
> I put this on some java forums and was told it wasn't possible. We have
> background processes that wake up and do some data processing. These
> processes are in java and connect through the JDBC.
>
> We have a shutdown script that will kill -14 these processes. The problem is
> that if they have performed DML(and have not committed), they commit when
> the process is killed. This leaves data in an erroneous state because all
> the processing was not finished and yet some of the dml was committed. We
> have a need to periodically shut down the application for maintenance.
>
> I can code a work around to wait for the processes to finish(create a
> shutdown_normal and a shutdown_immediate mimimicking oracle functionality),
> however, I am wondering if there is an API to tell Oracle to rollback
> instead of commit? I thought it was the autocommit, but it does not seem to
> work the same way as the one for sqlplus. All turning off the autocommit
> seems to do is stop every sql statement from immediately being committed.
>
> Thanks


--
--------------------------------------------------------------------
Another free oracle resource profiler
http://sourceforge.net/projects/oraresprof/
Now version 0.9
--
//www.freelists.org/webpage/oracle-l


Other related posts: