CscTransaction.java 3.69 KB
Newer Older
Naung1 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
/*
 * Created on 13 ¡.¾. 2550
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.csc.library.database;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;

import com.csc.library.utilities.UProfile;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class CscTransaction {
	private boolean rollback 	= false;
	private HashMap<String, CscConnection> connection 	= new HashMap<String, CscConnection>();
	public int beginTransactionKey	= 0; 
	public String userKey 					= "";
	
	public void rollback() throws SQLException {
		for (Iterator<String> it = connection.keySet().iterator(); it.hasNext();) {
			CscConnection con = connection.get(it.next());
			con.rollback();
			con.setCscTransaction(null);
			con.close();
		}
		connection.clear();
		this.rollback = true;
	}
	
	public void commit() throws SQLException {
		for (Iterator<String> it = connection.keySet().iterator(); it.hasNext();) {
			CscConnection con = connection.get(it.next());
//			con.commit();
			con.setCscTransaction(null);
			con.close();
		}
		connection.clear();
	}
	
	public void close() throws SQLException {
		for (Iterator<String> it = connection.keySet().iterator(); it.hasNext();) {
			CscConnection con = connection.get(it.next());
			con.setCscTransaction(null);
			con.close();
		}
		connection.clear();
	}
	
//  comment by pas à¹×èͧ¨Ò¡ 1 process 1 connection ·ÓãËéäÁèµéͧ stamp actiontime	
//	public void setActionTime()  {
//		for (Iterator<String> it = connection.keySet().iterator(); it.hasNext();) {
//			CscConnection con = connection.get(it.next());
//			con.setStarttime(System.currentTimeMillis());
//		}
//	}
	
	public CscConnection getConnection(String dbName, Object userKey) throws SQLException {
		CscConnection con = null;
		if (!this.isRollback()){
			String db = dbName.toLowerCase();
			if (connection.containsKey(db)) {
				con = connection.get(db); 
				if(con==null||(con!=null&&con.isClosed())){/** ¶éÒ connection ·Õè close ÃÐËÇèÒ§¡Ò÷ӧҹãËé·Ó¡Òà remove ·Ôé§ **/
				   if(con!=null){
					   con.closeAll();/**close connection ·Ôé§ä»¡Ã³Õ·ÕèÁѹäÁèÊÒÁÒöãªé§Ò¹connection ¹Ñé¹ä´é**/
					   con=null;
				   }
				   this.connection.remove(db);
				   con=getConnection(dbName,userKey);
			   }
			} else {
				/** 
				 * ¡Ã³Õ·ÕèÁÕ¡ÒÃÊÃéÒ§ connection ãËÁèãËéä»get ¨Ò¡ connectDB áÅéǶéÒà»ç¹ Connection process ¨Ð·Ó¡Òà setProcessid à¢éÒä»ÍÕ¡¤ÃÑé§ËÒ¡àÃÕ¡
				 * StaticProperties.myConnection.getConnection(db,userKey) ¨Ðà¡Ô´ recursive ËÒ¡äÁèset process id ¢Í§ process ÁռšѺ¡Ò÷ӧҹ¢Í§ connectionPools 
				*/
				con = (CscConnection) StaticProperties.myConnection.getConnection(db);
				synchronized(con) {
					UProfile up=(UProfile) userKey;
					if(up.getProcessid().length()>0){
						con.setProcessid(up.getProcessid());
					}
					this.connection.put(db, con);
					con.setAutoCommit(false);
					if(con.getTransactionIsolation()!=Connection.TRANSACTION_READ_UNCOMMITTED){
						con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);	
					}
					con.setUses(true);
					con.setCscTransaction(this);
				}	
			}
		}
		if(con!=null){
			con.setLasttime(System.currentTimeMillis());
		}
		return con;
	}
	
	public boolean isRollback() {
		return this.rollback;
	}

	public int getBeginTransactionKey() {
		return beginTransactionKey;
	}

	public void setBeginTransactionKey(int beginTransactionKey) {
		this.beginTransactionKey = beginTransactionKey;
	}

	public String getUserKey() {
		return userKey;
	}

	public void setUserKey(String userKey) {
		this.userKey = userKey;
	}
}