1 package com.daffodilwoods.daffodildb.server.serversystem; 2 3 import java.sql.*; 4 import java.util.*; 5 import javax.transaction.xa.*; 6 import com.daffodilwoods.database.resource.*; 7 8 public class DXAResource implements _DXAResource { 9 public static final int TMENDRSCAN = 8388608; 10 public static final int TMFAIL = 536870912; 11 public static final int TMJOIN = 2097152; 12 public static final int TMNOFLAGS = 0; 13 public static final int TMONEPHASE = 1073741824; 14 public static final int TMRESUME = 134217728; 15 public static final int TMSTARTRSCAN = 16777216; 16 public static final int TMSUCCESS = 67108864; 17 public static final int TMSUSPEND = 33554432; 18 public static final int XA_RDONLY = 3; 19 public static final int XA_OK = 0; 20 21 private Hashtable dxconnPool ; 22 private _ServerSystem serverSystem; 23 private int client = 0; 24 private _Connection connection; 25 26 public DXAResource( _ServerSystem serverSystem0) { 27 dxconnPool = new Hashtable(); 28 serverSystem = serverSystem0; 29 } 30 31 public synchronized _Connection start( Xid xid, int flags , Properties prop) throws XAException { 32 _Connection dxConn = null; 33 if ( xid == null ) 34 throw new XAException( XAException.XAER_INVAL ); 35 if ( flags == TMNOFLAGS ) { 36 if ( getDXConnection(xid) != null ) 37 throw new XAException( XAException.XAER_DUPID ); 38 if( dxconnPool.contains(xid) ) 39 throw new XAException( XAException.XAER_DUPID ); 40 try { 41 dxConn = getNewDXConnection( xid , prop ) ; 42 } catch ( SQLException except ) { 43 except.printStackTrace(); 44 throw new XAException( XAException.XAER_RMERR ); 45 } 46 } 47 else if ( flags == TMJOIN || flags == TMRESUME ) { 48 dxConn = getDXConnection(xid); 49 if ( dxConn == null ) 50 throw new XAException( XAException.XAER_INVAL ); 51 if ( flags == TMJOIN && ((DxConnection)dxConn).getClientCount() == 0 ) { 52 client++; 53 return dxConn; 54 } 55 ((DxConnection)dxConn).setClientCount( ((DxConnection)dxConn).getClientCount()+1); 56 } else 57 throw new XAException( XAException.XAER_INVAL ); 58 client++; 59 return dxConn ; 60 } 61 62 63 public synchronized void end( Xid xid, int flags ) throws XAException { 64 if ( xid == null ) 65 throw new XAException( XAException.XAER_INVAL ); 66 _Connection _dxConn = (_Connection)dxconnPool.get(xid); 67 if ( _dxConn == null ) { 68 throw new XAException( XAException.XAER_NOTA ); 69 } else { 70 ((DxConnection)_dxConn).setClientCount( ((DxConnection)_dxConn).getClientCount()-1); 71 } 72 client--; 73 } 74 75 76 public synchronized void forget ( Xid xid ) throws XAException { 77 client = 0; 78 if ( xid == null ) 79 throw new XAException( XAException.XAER_INVAL ); 80 setDxConnection( xid, null ); 81 } 82 83 public int prepare( Xid xid ) throws XAException { 84 if ( xid == null ) 85 throw new XAException( XAException.XAER_INVAL ); 86 DxConnection dxConn = ((DxConnection)dxconnPool.get(xid)); 87 if ( dxConn == null ) 88 throw new XAException( XAException.XAER_NOTA ); 89 if ( dxConn.clientCount > 0 ) 90 throw new XAException( XAException.XAER_PROTO ); 91 try { 92 if ( dxConn.connection.prepare() ) 93 return XA_OK; 94 else 95 return XA_RDONLY; 96 } catch ( DException except ) { 97 throw new XAException( XAException.XA_RBROLLBACK ); 98 } 99 } 100 101 public void commit( Xid xid ) throws XAException { 102 DxConnection dxConn = ((DxConnection)dxconnPool.get(xid)) ; 103 if ( dxConn == null ){ 104 throw new XAException( XAException.XAER_NOTA ); 105 } 106 try { 107 dxConn.commit(); 108 } catch ( DException except ) { 109 except.printStackTrace(); 110 try { 111 dxConn.connection.close(); 112 } catch (DException de) { 113 throw new XAException(de.getMessage()); 114 } 115 dxConn = null; 116 throw new XAException( XAException.XA_HEURRB ); 117 } 118 } 119 120 public int makePersistent( Xid xid ) throws XAException { 121 try { 122 DxConnection dxConn = (DxConnection)dxconnPool.get(xid); 123 if(dxConn.makePersistent()) { 124 return XA_OK ; 125 } 126 } catch(DException dbe) { 127 throw new XAException(dbe.getMessage()); 128 } 129 return -1; 130 } 131 132 public synchronized void rollback( Xid xid ) throws XAException { 133 _Connection dxConn = getDXConnection( xid ); 134 if ( dxConn == null ) 135 throw new XAException( XAException.XAER_NOTA ); 136 try { 137 dxConn.rollback(); 138 } catch ( DException except ) { 139 try { 140 dxConn.close(); 141 } catch (DException de) { 142 throw new XAException(de.getMessage()); 143 } 144 } finally { 145 forget( xid ); 146 } 147 } 148 149 public _Connection setDxConnection( Xid xid, _Connection dxConn ) { 150 if ( dxConn == null ) 151 return (_Connection) dxconnPool.remove( xid ); 152 else 153 return (_Connection) dxconnPool.put( xid, dxConn ); 154 } 155 156 HashMap connectionVSdbName = new HashMap(); public _Connection getNewDXConnection( Xid xid , Properties prop) throws SQLException { 158 String dbName0 = ((String )prop.get(in.co.daffodil.db.jdbc.DaffodilDBDriver.DATABASE_NAME_PROPERTY)).trim(); 159 prop.setProperty(_Server.XID,xid.toString()); 160 connection = (_Connection)connectionVSdbName.get(dbName0); 161 DxConnection connObjectToReturn = null; 162 try { 163 _Connection conn = connection != null ? connection : (connection = serverSystem.getConnection( (String )prop.get(in.co.daffodil.db.jdbc.DaffodilDBDriver.DATABASE_NAME_PROPERTY) , prop )); 164 connectionVSdbName.put(dbName0,conn); 165 if (connection == null ) 166 conn.setTransactionIsolation(java.sql.Connection.TRANSACTION_READ_COMMITTED); 167 connObjectToReturn = new DxConnection(conn); 168 connObjectToReturn.setClientCount(1) ; 169 dxconnPool.put(xid,connObjectToReturn ); 170 return conn; 171 } catch( DException e ){ 172 e.printStackTrace(); 173 throw e.getSqlException(null); 174 } 175 } 176 177 public void close() { 178 connection = null; 179 } 180 181 public _Connection getDXConnection( Xid xid ){ 182 return (_Connection) dxconnPool.get(xid); 183 } 184 public synchronized boolean inGlobalTransaction() { 185 return client > 0; 186 } 187 } 188 | Popular Tags |