• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Transaction handling with Stateless SessionBean

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am using stateless session bean in my application (since I do not require to save state). I am calling my DAO ,methods from the bean. In some cases multiple interaction with databse are happening in DAO method, for Example,

1. Table X1 is updated for certain 'where' criteria.
2. Records from Table X2 is deleted for same 'where' criteria.
3. new updated record is/are inserted into Table X2 for same primary key for which data had been deleted.

In such case if step 2 or 3 fails then whole transaction should rollback.
I used bean as container managed (so I can not make con.setAutoCommit(false), can not handle transaction manually and I do not want if container can handle by it's own).

I am using App server as JBoss 4.0.5 and making connection using initialContext.lookup("aaa") where aaa is JNDI name mentioned in JBOSS file.

My doubt is, could it be possible to manage the transaction with using stateless session bean. If not then how with Stateful session bean (I may use stateful as well)

Thanks
Shalindra
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Study the details of ejb-jar.xml and discover what the container-transaction and trans-attribute elements do for CMT.
[ August 21, 2008: Message edited by: James Clark ]
 
shalindra Singh Suryvanshee
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for suggestion!
I gone through it.

I could very very new to EJB's
I am sending you detail of code, if you can help me why it is not working !

"ejb-jar.xml"

<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>SupplierDetailsEJB</ejb-name> <home>com.ejb.functionalmgr.supplierdetails.SupplierDetailsHome</home><remote>com.ejb.functionalmgr.supplierdetails.SupplierDetailsRemote</remote>
<ejb-class>com.ejb.functionalmgr.supplierdetails.SupplierDetailsBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>SupplierDetailsEJB</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Supports</trans-attribute>
</container-transaction>
<container-transaction>
<method>
<ejb-name>SupplierDetailsEJB</ejb-name>
<method-name>updateSupplierDetails</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>

"Bean Class"

public void updateSupplierDetails(SupplierDetailsVO supplierDetailsVO, String currentRole) throws RemoteException, SQLException, SystemException {

Connection con = null;
try {
con = ConnectionPool.getConnection();
SupplierDetailsDAO supplierDetailsDAO = new SupplierDetailsDAOImpl();
supplierDetailsDAO.updateSupplierDetails(con, supplierDetailsVO, currentRole);
}
catch (SQLException e) {
throw new SQLException(e.getMessage());

}
catch (Exception e) {
throw new RemoteException(e.getMessage());

}
finally {

ConnectionPool.closeConnection(con);
}

}

supplierDetailsDAO.updateSupplierDetails(con, supplierDetailsVO, currentRole) methode having one update in one table, delete from another table and insertion in the same table depending upon certain criteria.

Now if any of above query fails (update delete insert), it is not rolling back fully.

Can you suggest me what wrong I am doing here!!!

Thanks
Shalindra Singh
SCJP, SCWCD
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This EJB method does a database (1) update record, (2) delete record, and (3) insert record.

What operation fails? ... and why?


What does the following mean?

<session-type>Stateful</session-type>


[ August 25, 2008: Message edited by: James Clark ]
 
shalindra Singh Suryvanshee
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI James!

probably My msg conveyed wrong to you.
It is not any DAO methods which are failing or creating problem.
I am asking if I explicitely throw error in (say insertion) then whole transation should rollback. but it is not happening. First two operations 1. Updation 2) Deletion persist in a database while all three operations are in same method (Which mean within single transaction unit, I think).

I am asking if there is something wrong in my code of something missing to achieve above functionality?

Thanks
Shalindra
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you asking if a rollback is possible for joined transactions?

If so, then we could look at it having a remote interface that acts as a facade calling three business methods where the first business method has REQUIRED transaction attribute, that other having SUPPORTS transaction attribute, that way when any exception is thrown, it would rollback the whole transaction.

Im not sure why your business method is stateful when you are using asking for a stateless scenario. Also, the SUPPORTS transaction attribute implies a previous transaction is already existing, else any transactional related functions executed in a method without transaction will cause an error.

Regards.
 
shalindra Singh Suryvanshee
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes ! I want to rollback the transaction if any exception occur.
I tried with using 'stateless' or 'stateful'.
I posted code above, I wanted to know whether above code will OK for making rollback if any exception occur, if not please suggest me the way by which I may achieve it.

If I remove SUPPORT section from ejb-jar.xml, even though it is not working.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am asking if I explicitely throw error in (say insertion) then whole transation should rollback. but it is not happening.



In your updateSupplierDetails(...) method above, where does "say insertion" occur?

Are you purposely creating an error? If so, where is the code that is creating the error?

"explicitly" is not spelled with two "e", only one
 
shalindra Singh Suryvanshee
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry to bothering you...
Give up...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic