The Component Implementation Code Comparison
Implementing the Server Component
MTS server component- All the classes that are required for Java/COM are defined in the com.ms.com package. The com.ms.mtx package contains all the MTS related class definitions. The com.ms.wfc.data package contains all the ADO related class definitions. The MTS Server object shown below implements the IChecking interface that we defined in our IDL file. Implementation code for the different methods like createAccount(), credit(), debit(), getBalance() and getCustomerName() are also shown.
In MTS, the developer implements the business methods, but doesn't have to implement any callback methods. The developer must code transaction commit and rollback demarcation statements. If everything goes right, the developer must call SetComplete() on the transaction context object. If anything goes wrong, the developer must call SetAbort() on the transaction context object. MTS supports four automatic transaction semantics which may de assigned declaratively. MTS does not support manual transactions.
EJB server component- All the classes that are required for Java/RMI are defined in the java.rmi package. The javax.ejb package contains all the EJB related class definitions. The java.sql package contains all the JDBC related class definitions. The EJB Server object shown below implements the SessionBean interface since its a session bean. Implementation code for the different methods like createAccount(), credit(), debit(), getBalance() and getCustomerName() are also shown.
In EJB, the transaction semantics for an enterprise bean are defined declaratively rather than programmatically. At runtime, the EJB container automatically implements transaction services according to the TransactionAttribute specified in the deployment descriptor. In EJB, the developer must define the remote interface, and the developer must implement the business methods and the EJB callback methods (although in many cases they can be left empty). The EJB container deployment tools automatically generate the EJB wrapper objects.
MTS - Component implementation code |
EJB - enterprise Bean implementation Code |
package
bank; import com.ms.com.*; |
package
Bank; import java.rmi.*; |
/**
@com.register(clsid=6B6EBD42-01C0-11D3-97EE-006097A7D34F,
* typelib=6B6EBD40-01C0-11D3-97EE-006097A7D34F, version="1.0") * @com.transaction (required) */ //////////////////////////////////////////////////////////////// // Class Definition //////////////////////////////////////////////////////////////// public class CheckingImpl implements IUnknown, com.ms.com.NoAutoScripting, bank.ICheckingDefault { //////////////////////////////////////////////////////////////// static final int ALL_FIELDS = 0; static final int NAME_FIELD = 1; static final int BALANCE_FIELD= 2; //odbc type connection string static final String m_strOpenConn = "PROVIDER=Microsoft.Jet.OLEDB.3.51;"+ "Data Source=E:\\MyProjects\\AccountMTS\\BankServer\\Bank.mdb"; |
//////////////////////////////////////////////////////////////////
// Class Definition ////////////////////////////////////////////////////////////////// public class CheckingBean implements SessionBean { ////////////////////////////////////////////////////////////////// protected SessionContext _sessionContext; static final int ALL_FIELDS = 0; static final int NAME_FIELD = 1; static final int BALANCE_FIELD= 2; ////////////////////////////////////////////////////////////////// // Home Interface methods ////////////////////////////////////////////////////////////////// public void ejbCreate () { } |
////////////////////////////////////////////////////////////////
// Business methods //////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// public void createAccount (int key, String name, double startingBalance) { //////////////////////////////////////////////////////////////// if (startingBalance < 0) return; boolean bSuccess = false; IObjectContext context = null; try { // Get the Object Context context = (IObjectContext)MTx.GetObjectContext (); truePut (CheckingImpl.ALL_FIELDS, key, (new Double (startingBalance)).toString (), name); bSuccess = true; } catch (Exception e) { bSuccess = false; e.printStackTrace (); } // Upon exit, always call SetComplete () if happy, // or SetAbort () if unhappy // We do this since we never save state across method calls. finally { if (context!=null) { if (bSuccess == true) context.SetComplete (); else context.SetAbort (); } } } |
//////////////////////////////////////////////////////////////////
// Remote Interface methods ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// public void createAccount (int key, String name, double startingBalance) { ////////////////////////////////////////////////////////////////// if (startingBalance > 0) { truePut (CheckingBean.ALL_FIELDS, key, startingBalance, name); } } |
////////////////////////////////////////////////////////////////
public void credit (double amount, int key) { //////////////////////////////////////////////////////////////// double balance = 0.0; boolean bSuccess = false; try { balance = getBalance (key); } catch (Exception e) { e.printStackTrace (); } System.out.println("changing Checkings::balance from " + balance);
// Get the Object Context |
//////////////////////////////////////////////////////////////// public void credit (double amount, int key) { //////////////////////////////////////////////////////////////// double balance = 0.0; try { balance = this.getBalance (key); } catch (Exception e) { e.printStackTrace (); } System.out.println ("changing Checking::balance from " + balance); try { |
////////////////////////////////////////////////////////////////
public void debit (double amount, int key) { //////////////////////////////////////////////////////////////// double balance = 0.0; boolean bSuccess = false; try { balance = getBalance (key); } catch (Exception e) { e.printStackTrace (); } System.out.println ("changing Checkings::balance from " + balance);
// Get the Object Context |
//////////////////////////////////////////////////////////////// public void debit (double amount, int key) { //////////////////////////////////////////////////////////////// double balance = 0.0; try { balance = getBalance (key); } catch (Exception e) { e.printStackTrace (); } System.out.println("changing CheckingBean::balance from " + balance); try { if ( (amount > 0) && (balance >= amount) ) { balance -= amount; truePut (CheckingBean.BALANCE_FIELD, key, balance, ""); } } catch (Exception e) { e.printStackTrace (); } System.out.println(" to " + balance); } |
////////////////////////////////////////////////////////////////
public double getBalance (int key) { //////////////////////////////////////////////////////////////// double balance = 0.0; boolean bSuccess= false; System.out.println ("Invoking Checkings::getBalance"); IObjectContext context = null; try { // Get the Object Context context = (IObjectContext)MTx.GetObjectContext (); String result = trueGet (CheckingImpl.BALANCE_FIELD, key); balance = (new Double (result)).doubleValue(); bSuccess = true; } catch (Exception e) { bSuccess = false; e.printStackTrace (); } // Upon exit, always call SetComplete () if happy, // or SetAbort () if unhappy // We do this since we never save state across method calls. finally { if (context!=null) { if (bSuccess == true) context.SetComplete (); else context.SetAbort (); } } System.out.println ("Checkings::balance is " + balance); return balance; } |
//////////////////////////////////////////////////////////////// public double getBalance (int key) { //////////////////////////////////////////////////////////////// double balance = 0.0;
try { |
////////////////////////////////////////////////////////////////
public String getCustomerName (int key) { //////////////////////////////////////////////////////////////// String name = null; boolean bSuccess= false; System.out.println ("Invoking Checkings::getCustomerName"); IObjectContext context = null; try { // Get the Object Context context = (IObjectContext)MTx.GetObjectContext (); name = trueGet (CheckingImpl.NAME_FIELD, key); bSuccess = true; } catch (Exception e) { bSuccess = false; e.printStackTrace (); } // Upon exit, always call SetComplete () if happy, // or SetAbort () if unhappy // We do this since we never save state across method calls. finally { if (context!=null) { if (bSuccess == true) context.SetComplete (); else context.SetAbort (); } } System.out.println ("Checkings::customer_name is " + name); return name; } |
//////////////////////////////////////////////////////////////// public String getCustomerName (int key) { //////////////////////////////////////////////////////////////// String name = null; boolean bSuccess= false; try { name = trueGet (CheckingBean.NAME_FIELD, key); } catch (Exception e) { e.printStackTrace (); } System.out.println ("Checking::customer_name is " + name); return name; } |
////////////////////////////////////////////////////////////////
// Other Internal methods //////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// String trueGet (int type, int key) { //////////////////////////////////////////////////////////////// Connection connection = null; Recordset rset = null; Variant rowCount = new Variant (); String result = null; String query = null; String fieldName = null; switch (type) { case CheckingImpl.NAME_FIELD: query = "SELECT CUSTOMER_NAME FROM Checkings WHERE ACCOUNT_NUMBER="; fieldName = "CUSTOMER_NAME"; break; case
CheckingImpl.BALANCE_FIELD: |
//////////////////////////////////////////////////////////////// // Other Internal methods //////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// String trueGet (int type, int key) { //////////////////////////////////////////////////////////////// Connection connection = null; PreparedStatement stmt= null; ResultSet rset = null; String result = null; String query = null; String fieldName = null; switch (type) { case CheckingBean.NAME_FIELD: query = "SELECT CUSTOMER_NAME FROM Checkings "+ "WHERE ACCOUNT_NUMBER = ?"; fieldName = "CUSTOMER_NAME"; break; case
CheckingBean.BALANCE_FIELD: |
////////////////////////////////////////////////////////////////
String truePut (int type, int key, String balance, String name) { //////////////////////////////////////////////////////////////// Connection connection = null; Recordset rset = null; Variant rowCount = new Variant (); String result = null; String query = null; switch (type) { case CheckingImpl.BALANCE_FIELD: query = "UPDATE Checkings SET BALANCE=" + balance + " WHERE ACCOUNT_NUMBER="+ key; break; case
CheckingImpl.NAME_FIELD: } |
//////////////////////////////////////////////////////////////// String truePut (int type, int key, double balance, String name) { //////////////////////////////////////////////////////////////// Connection connection = null; PreparedStatement stmt= null; ResultSet rset = null; String result = null; String query = null; switch (type) { case CheckingBean.BALANCE_FIELD: query = "UPDATE Checkings SET BALANCE = ? "+ "WHERE ACCOUNT_NUMBER = ?"; break; case
CheckingBean.NAME_FIELD: |
//////////////////////////////////////////////////////////////// public Connection getConnection () throws Exception { //////////////////////////////////////////////////////////////// return DriverManager.getConnection ("jdbc:odbc:Bank"); } |
|
//////////////////////////////////////////////////////////////// public CheckingBean () { //////////////////////////////////////////////////////////////// } //////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// } |
|
CheckingImpl.java |
CheckingBean.java |
click here to go
to My Advanced Java / J2EE Tutorial HomePage... |
This site was developed and is maintained by Gopalan Suresh Raj This page has been visited times since May 04,1999. |
Last Updated : May 04,'99 |
Copyright (c) 1997-2000, Gopalan Suresh Raj - All rights reserved. Terms of use. |
All products and companies mentioned at this site are trademarks of their respective owners. |