Saturday, September 27, 2014

Transaction Control Design pattern sample

package com.db2;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;


public class BasicDataSourceExample {
    public static void main(String[] args) throws Exception {
    long t1=0;
         long t2=0;
         t1=System.currentTimeMillis();
        //
        // Creates a BasicDataSource and defines its properties
        // including the driver class name, jdbc url, username
        // and password.
        //
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.ibm.db2.jcc.DB2Driver");
        dataSource.setUrl("jdbc:db2://localhost:50000/mount:currentSchema=mount;currentFunctionPath=mount;");
        dataSource.setUsername("db2admin");
        dataSource.setPassword("db2admin");

        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            //
            // Get a connection from the data source and do some
            // database query with the obtained connection.
            //
            conn = dataSource.getConnection();
            stmt = conn.prepareStatement("select * from Evolution");
            ResultSet rs = stmt.executeQuery();
            t2=System.currentTimeMillis();
            System.out.println(t2-t1);
         
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        }
    }
}


package com.db2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

class TransactionObject {

    private static Connection conn;
   

    TransactionObject() {
        try {
            String url = "jdbc:db2://localhost:50000/mount:currentSchema=mount;currentFunctionPath=mount;";
            conn = DriverManager.getConnection(url, "db2admin", "db2admin");
        } catch (SQLException e) {

        }

    }

    /**
     * @return the conn
     */
    public static Connection getConn() {
        return conn;
    }

   

    void rollBack() {
        try {
            conn.rollback();
        } catch (SQLException e) {

        }
    }

    void Commit() {
        try {
            conn.commit();
        } catch (SQLException e) {

        }

    }



}


package com.db2;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Demo {

PreparedStatement pstmt;
ResultSet rsett;
Connection connection = null;

public void execute() {

try {
Class.forName("com.ibm.db2.jcc.DB2Driver");

// ...
try {

pstmt = connection.prepareStatement("SELECT * from Evolution");
} catch (SQLException exception) {

}

try {
rsett = pstmt.executeQuery();
System.out.println("how many times i am executed");

} catch (SQLException exception) {

}

return;
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
}

void Method() {
// Demo dm = new Demo();
// dm.dataConnection();
ExecutorService executor = Executors.newFixedThreadPool(20);
List<Thread> callingList = new ArrayList<Thread>();
Object thread=new Object();
for (int i = 0; i < 20; i++) {
Thread localImpl =new Thread(thread);
callingList.add(localImpl);
}
List<Future<Object>> results1 = null;

try {

results1 = executor.invokeAll(callingList);

} catch (InterruptedException e) {

}

}
public static void main(String[] args) {
Demo test=new Demo();
test.Method();
}

public class Thread implements Callable<Object> {
private TransactionObject tranobject=new TransactionObject();
private ConnectionObject object=new ConnectionObject(tranobject);
private Demo demo=new Demo();
private Object objectcommon;
private  int  threadno;

@Override
public Object call() throws Exception {
threadno++;
if(threadno>18){
System.out.println("threadexceeded");
}
demo.execute();
return new Object();
}
Thread(Object object) {
this.objectcommon = objectcommon;

}

}

public class ConnectionObject {
private TransactionObject transactionobject;

ThreadLocal<ConnectionObject> threadLocal = new ThreadLocal<ConnectionObject>() {

@Override
protected ConnectionObject initialValue() {

return new ConnectionObject(transactionobject);
}
};

ConnectionObject(TransactionObject transactionobject) {
this.transactionobject = transactionobject;

}

}

}

No comments:

Post a Comment