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
}
Roedy Green - 16 Oct 2007 02:17 GMT
On Mon, 15 Oct 2007 18:08:26 -0700, "teser3@hotmail.com"
<teser3@hotmail.com> wrote, quoted or indirectly quoted someone who
said :
> try
> {
[quoted text clipped - 8 lines]
> {
> connection.rollback();
Don't you need some sort of "begin transaction" call?

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
teser3@hotmail.com - 16 Oct 2007 02:23 GMT
On Oct 15, 9:17 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Mon, 15 Oct 2007 18:08:26 -0700, "tes...@hotmail.com"
> <tes...@hotmail.com> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 17 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com
Any example of what the exact "begin transaction" part looks like?
Arne Vajhøj - 16 Oct 2007 02:35 GMT
> On Mon, 15 Oct 2007 18:08:26 -0700, "teser3@hotmail.com"
> <teser3@hotmail.com> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 14 lines]
>
> Don't you need some sort of "begin transaction" call?
No.
Plain JDBC does not use such.
Arne
Arne Vajhøj - 16 Oct 2007 02:37 GMT
> 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)
> {
[quoted text clipped - 13 lines]
> return one;
> }
> public int methodTwo(City city)
> {
[quoted text clipped - 13 lines]
> return two;
> }
> public int mainInsert(City city)
> {
[quoted text clipped - 22 lines]
> Connection
> }
Does all 3 methods have the same connection object ?
Arne
Real Gagnon - 16 Oct 2007 02:43 GMT
> 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.

Signature
Real Gagnon from Quebec, Canada
* Java, Javascript, VBScript and PowerBuilder code snippets
* http://www.rgagnon.com/howto.html
* http://www.rgagnon.com/bigindex.html
teser3@hotmail.com - 16 Oct 2007 23:56 GMT
> "tes...@hotmail.com" <tes...@hotmail.com> wrote innews:1192496906.485246.74640@k35g2000prh.googlegroups.com:
>
[quoted text clipped - 19 lines]
> *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!