Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Mailing Lists -> Oracle-L -> Re: jdbc and kill -14

Re: jdbc and kill -14

From: Giovanni Cuccu <giovanni.cuccu_at_gmail.com>
Date: Wed, 29 Mar 2006 13:47:11 +0200
Message-ID: <23e0d1170603290347l67e8164ah74bd461960ff6f1d@mail.gmail.com>


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

    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_at_comcast.net <ryan_gaffuri_at_comcast.net> 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
--
http://www.freelists.org/webpage/oracle-l
Received on Wed Mar 29 2006 - 05:47:11 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US