java Finalize method call

Posted by Rajesh Kumar J on Stack Overflow See other posts from Stack Overflow or by Rajesh Kumar J
Published on 2010-03-24T09:46:30Z Indexed on 2010/03/24 9:53 UTC
Read the original article Hit count: 505

Filed under:
|
|
|

The following is my Class code

import java.net.*;
import java.util.*;
import java.sql.*;
import org.apache.log4j.*;
class Database {
    private Connection conn;
    private org.apache.log4j.Logger log ;
    private static Database dd=new Database();
    private Database(){
        try{
            log= Logger.getLogger(Database.class);
            Class.forName("com.mysql.jdbc.Driver");
            conn=DriverManager.getConnection("jdbc:mysql://localhost/bc","root","root");
            conn.setReadOnly(false);
            conn.setAutoCommit(false);
            log.info("Datbase created");
            /*Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn=DriverManager.getConnection("jdbc:odbc:rmldsn");
            conn.setReadOnly(false);
            conn.setAutoCommit(false);*/

        }
        catch(Exception e){
            log.info("Cant Create Connection");
        }
    }
    public static Database getDatabase(){
        return dd;
    }
    public Connection getConnection(){
        return conn;
    }
    @Override
    protected void finalize()throws Throwable {
        try{
        conn.close();
        Runtime.getRuntime().gc();
        log.info("Database Close");
        }
        catch(Exception e){
            log.info("Cannot be closed Database");
        }
        finally{
            super.finalize();
        }        
    }
}

This can able to Initialize Database Object only through getDatabase() method. The below is the program which uses the single Database connection for the 4 threads.

public class Main extends Thread {
    public static int c=0;
    public static int start,end;
    private int lstart,lend;
    public static Connection conn;
    public static Database dbase;
    public Statement stmt,stmtEXE; public ResultSet rst;
    /**
     * @param args the command line arguments
     */
    static{        
        dbase=Database.getDatabase();
        conn=dbase.getConnection();
    }
    Main(String s){
        super(s);
        try{
        stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
       start=end;
       lstart=start;
       end=end+5;
       lend=end;
        System.out.println("Start -" +lstart +" End-"+lend);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void run(){
       try{

            URL url=new URL("http://localhost:8084/TestWeb/");


    rst=stmt.executeQuery("SELECT * FROM bc.cdr_calltimestamp limit "+lstart+","+lend);

        while(rst.next()){

        try{
            rst.updateInt(2, 1);
            rst.updateRow();
            conn.commit();
            HttpURLConnection httpconn=(HttpURLConnection) url.openConnection();
            httpconn.setDoInput(true);
            httpconn.setDoOutput(true);
            httpconn.setRequestProperty("Content-Type", "text/xml");
            //httpconn.connect();

            String reqstring="<?xml version=\"1.0\" encoding=\"US-ASCII\"?>"+
                "<message><sms type=\"mt\"><destination messageid=\"PS0\"><address><number" +
                "type=\"international\">"+ rst.getString(1) +"</number></address></destination><source><address>" +
                "<number type=\"unknown\"/></address></source><rsr type=\"success_failure\"/><ud" +
                "type=\"text\">Hello World</ud></sms></message>";
            httpconn.getOutputStream().write(reqstring.getBytes(), 0, reqstring.length());

            byte b[]=new byte[httpconn.getInputStream().available()];
            //System.out.println(httpconn.getContentType());
            httpconn.getInputStream().read(b);
            System.out.println(Thread.currentThread().getName()+new String(" Request"+rst.getString(1)));
            //System.out.println(new String(b));
            httpconn.disconnect();
            Thread.sleep(100);

            }
         catch(Exception e){
            e.printStackTrace();
        }

       }
            System.out.println(Thread.currentThread().getName()+"  "+new java.util.Date());
       }
       catch(Exception e){
           e.printStackTrace();
       }

    }

    public static void main(String[] args) throws Exception{
       System.out.println(new java.util.Date());
       System.out.println("Memory-before "+Runtime.getRuntime().freeMemory());
        Thread t1=new Main("T1-");
        Thread t2=new Main("T2-");
        Thread t3=new Main("T3-");
        Thread t4=new Main("T4-");
        t1.start();
        t2.start();
        t3.start();
        t4.start();

        System.out.println("Memory-after "+Runtime.getRuntime().freeMemory());


    }

}

I need to Close the connection after all the threads gets executed. Is there any good idea to do so. Kindly help me out in getting this work.

© Stack Overflow or respective owner

Java Finalize method call

Posted by Rajesh Kumar J on Stack Overflow See other posts from Stack Overflow or by Rajesh Kumar J
Published on 2010-03-24T09:24:19Z Indexed on 2010/03/24 9:33 UTC
Read the original article Hit count: 505

Filed under:
|
|
|

I need to find when finalized method called in the JVM. I Created a test Class which write into file when finalized method called by Overriding the protected finalize method It is not executing. Can anybody tell me the reason why it is not executing?? Thanks in Advance

© Stack Overflow or respective owner

Related posts about java

Related posts about finalize