Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Transaction in session bean not working

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I gave the Transaction attribute to required for all the methods in the bean.
public void insertmethod()
{
DBUtil db = new DBUtil();
db.executeUpdate(String1);
db.executeUpdate(String2);
db.close();
}
public class DBUtil {
Connection con
DBUtiil()
con = .....
public void executeUpdate {
Get prepared statement from above conn object and executeUpdate
}
}
We have written a utility class DBUtil where the connection object is obtained in the constructor.
we are executing two insert queries. If the first insert is sucessful and the second fails still the first insert is committed. We do not want this to happen. Is the error due to database transaction occurring in a different class other than the bean.
Could not figure out what Iam missing. How to get the transaction to work??.
Thanks for your time
Regards,
Kishan
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please post the contents of the ejb-jar.xml file so that we can see how the transactions have been specified.
 
Balaji Munuswamy
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Attaching the ejb-jar.xml Thanks for your time.

Are we missing anything here.
Regards,
Balaji
Hi
Any non html tag needs to have space after < so that it is not considered a html tag by the browser
[ May 06, 2002: Message edited by: Rahul Mahindrakar ]
 
Balaji Munuswamy
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think the XML tags are not coming...what do I have to do to get the tags get displayed
Regards,
Balaji
 
Rahul Mahindrakar
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is pretty strange as it should have worked. The xml file looks ok. The transaction attribute of Required creates a new transaction if the method is not invoked in a transaction.
Then where is the problem
 
Rahul Mahindrakar
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is the error due to database transaction occurring in a different class other than the bean.


This is not a problem as long as you are not invoking any transactions in the other class
 
Balaji Munuswamy
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rahul,
Iam pasting my bean code. Please see whether Iam doing anything wrong. I have taken off the util class and doing the transaction from with in the bean itself. Still it does not work.
public void dirInsert(int,String,int,String) {
Connection con;
try {
InitialContext inCtx = new InitialContext();
DataSource dsConcert = (DataSource) inCtx.lookup("java:comp/env/jdbc/oracle");
if (dsConcert != null) {
con =
dsConcert.getConnection();
} else {
System.out.println(" Datasource is Null ");
throw new Exception("Null DataSource");
}
System.out.println("Got Connection Object");
StringBuffer strInsert1 = new StringBuffer();
strInsert1.append(" INSERT INTO CHK_TRNS1 VALUES( " + mno + ",'" + mname + "') ");

StringBuffer strInsert2 = new StringBuffer();
strInsert2.append(" INSERT INTO CHK_TRNS2 VALUES( " + mno1 + ",'" + mname1 + "') ");
String strIns1 = strInsert1.toString();
String strIns2 = strInsert2.toString();

Statement stmt = con.createStatement();
stmt.executeUpdate(strIns1);
stmt.executeUpdate(strIns2);

stmt.close();
con.close();
System.out.println("Successfully executed the Inserts ");
} catch(Exception e) {
System.out.println(" Exception is bean : " + e);
}
}
As said earlier...When second insert fails still the first insert commits.
Thanks for your help.
Regards,
Balaji.
 
Rahul Mahindrakar
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Balaji ,
I am not able to detect the problem
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic