Transaction rollback not working

T

teser3

I am trying to get Transaction working with JDBC and Oracle database.
In my below example, if I try and fail the second insert by putting in
an invalid table name (called BadTableName), the rollback doesnt work
because the first insert makes it into the database. Please advise how
I can get this to work:

public class Serra
{
.....
public PreparedStatement prep;
...

public int methodOne(City city)
{
int one = 0;
try
{
prep = connection.prepareStatement("insert into States
(fieldOne,fieldTwo) values (?,?)");
prep.setString(1, city.getFieldOne());
prep.setString(2, city.getFieldTwo());
prep.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
}
return one;
}


public int methodTwo(City city)
{
int two = 0;
try
{
prep = connection.prepareStatement("insert into BadTableName
(fieldThree,fieldFour) values (?,?)");
prep.setString(1, city.getFieldThree());
prep.setString(2, city.getFieldFour());
prep.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
}
return two;
}

....

public int mainInsert(City city)
{
try
{
connection.setAutoCommit(false);
methodOne(city);
methodTwo(city);
connection.commit();
}
catch (SQLException ex)
{
try
{
connection.rollback();
}
catch(Exception e)
{
e.printStackTrace();
}
ex.printStackTrace();
}
finally
{
//closing part here for the ResultSet, Statement and
Connection
}
 
R

Roedy Green

try
{
connection.setAutoCommit(false);
methodOne(city);
methodTwo(city);
connection.commit();
}
catch (SQLException ex)
{
try
{
connection.rollback();

Don't you need some sort of "begin transaction" call?
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

I am trying to get Transaction working with JDBC and Oracle database.
In my below example, if I try and fail the second insert by putting in
an invalid table name (called BadTableName), the rollback doesnt work
because the first insert makes it into the database. Please advise how
public int methodOne(City city)
{
int one = 0;
try
{
prep = connection.prepareStatement("insert into States
(fieldOne,fieldTwo) values (?,?)");
prep.setString(1, city.getFieldOne());
prep.setString(2, city.getFieldTwo());
prep.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
}
return one;
}
public int methodTwo(City city)
{
int two = 0;
try
{
prep = connection.prepareStatement("insert into BadTableName
(fieldThree,fieldFour) values (?,?)");
prep.setString(1, city.getFieldThree());
prep.setString(2, city.getFieldFour());
prep.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
}
return two;
}
public int mainInsert(City city)
{
try
{
connection.setAutoCommit(false);
methodOne(city);
methodTwo(city);
connection.commit();
}
catch (SQLException ex)
{
try
{
connection.rollback();
}
catch(Exception e)
{
e.printStackTrace();
}
ex.printStackTrace();
}
finally
{
//closing part here for the ResultSet, Statement and
Connection
}

Does all 3 methods have the same connection object ?

Arne
 
R

Real Gagnon

I am trying to get Transaction working with JDBC and Oracle database.
In my below example, if I try and fail the second insert by putting in
an invalid table name (called BadTableName), the rollback doesnt work
because the first insert makes it into the database. Please advise how
I can get this to work:

That's because you catch the Exception in methodTwo() so mainCity() is
never notified.

Remove the try/catch and change the signature to

public int methodTwo(City city)throws SQLException

(and do the same for methodOne()!)

Bye.
 
T

teser3

That's because you catch the Exception in methodTwo() so mainCity() is
never notified.

Remove the try/catch and change the signature to

public int methodTwo(City city)throws SQLException

(and do the same for methodOne()!)

Bye.
--
Real Gagnon from Quebec, Canada
* Java, Javascript, VBScript and PowerBuilder code snippets
*http://www.rgagnon.com/howto.html
*http://www.rgagnon.com/bigindex.html

Real Gagnon,

I hope you get this message. It works great and thanks for your input
and solving my problem!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top