EJB Best Practices Code Snippets
Date: 22-Jul-2002
1. Declarative Transaction - Sample
Code Snippet
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>HotelInformationHome</ejb-name>
<home>HotelInformationHome</home>
<remote>HotelInformation</remote>
<ejb-class>HotelInformationBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
transaction-type = Specify who will take care of transaction. Value
may be either Container or Bean.
2. Programmatic Transaction -
Sample Code Snippet
javax.transaction.UserTransaction userTransaction=m_ctx.getUserTransaction();
try{
userTransaction.begin();
...........................
......................... // Bussiness Logic
userTransaction.commit();
}catch(Exception e){
if(userTransaction!=null) userTransaction.rollback();
throw e;
}
m_ctx = javax.ejb.SessionContext
3. ejbStore With Boolean flag - Sample
Code Snippet
public void ejbStore() {
if(b_isDirty==true){
// Database update operations START
// .....
// .....
// Database update operations END
b_isDirty = false;
}
}
b_isDirty = boolean flag which controls database operations
|