Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7835529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:40:38+00:00 2026-06-02T13:40:38+00:00

I have a jdbc code in which I have used transaction management in code.

  • 0

I have a jdbc code in which I have used transaction management in code.
Following is the code.I am using Mysql Database.

public class JdbcConn {
public static void main(String[] args){
    Savepoint spt1 = null;
    Connection con = null;
    try{
        Class.forName("org.gjt.mm.mysql.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost","root","tany");
        con.setAutoCommit(false);
         spt1= con.setSavepoint("svpt1");

        PreparedStatement psmt;
        String query1 = "select city, countryid from querytest.city;";
        psmt=con.prepareStatement(query1);
        ResultSet rs=psmt.executeQuery();
        while(rs.next()){
            String query2 = "insert into sun.city (city,countryid) values('"+rs.getString(1)+"',"+rs.getInt(2)+");";
            psmt=con.prepareStatement(query2);
            psmt.executeUpdate();
        }
        String query3 = "create database `transtest`;";
        psmt=con.prepareStatement(query3);
        psmt.executeUpdate();

        String query4 = "CREATE TABLE `transtest`.`trans` (`id` tinyint(4) NOT NULL auto_increment,`val` int(5) NOT NULL default 0, PRIMARY KEY  (`id`)) ENGINE=MyISAM;";                
        psmt=con.prepareStatement(query4);
        psmt.executeUpdate();


        String query5 = "CREATE TABLE `transtest`.`transone` (`id` tinyint(4) NOT NULL auto_increment,`val` int(5) NOT NULL default 0, PRIMARY KEY  (`id`)) ENGINE=MyISAM;";                
        psmt=con.prepareStatement(query5);
        psmt.executeUpdate();


        String query6 = "CREATE TABLE `transtest`.`transtwo` (`id` tinyint(4) NOT NULL auto_increment,`val` int(5) NOT NULL default 0, PRIMARY KEY  (`id`)) ENGINE=MyISAM;";                
        psmt=con.prepareStatement(query6);
        psmt.executeUpdate();

        for(int i=1;i<=10;i++){
            String query7 = "insert into `transtest`.`transtwo` (`val`) values ("+i*2+");";                
            psmt=con.prepareStatement(query7);
            psmt.executeUpdate();
        }

        String query8 = "insertd into `transtest`.`trans` (`val`) values (500);";                
        psmt=con.prepareStatement(query8);
        psmt.executeUpdate();
        JOptionPane.showMessageDialog(null, "Process completed!");
        con.commit();
        con.setAutoCommit(true);

    }catch(SQLException sqle){
        try {
            con.rollback(spt1);
            JOptionPane.showMessageDialog(null, "Rollback1!");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        sqle.getMessage();
        sqle.printStackTrace();
    }catch (ClassNotFoundException cnfe) {
        // TODO Auto-generated catch block
        try {
            con.rollback(spt1);
            JOptionPane.showMessageDialog(null, "Rollback2!");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        cnfe.getMessage();
        cnfe.printStackTrace();
    }catch (Exception e) {
        // TODO Auto-generated catch block
        try {
            con.rollback(spt1);
            JOptionPane.showMessageDialog(null, "Rollback3!");
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        e.getMessage();
        e.printStackTrace();
    }

}   

}

This above code is not rolling back when sql exception comes.
The schema of the table in query1 and the schema of the table in query2 is same but as you see the databases are different.

I just dont know if any exception comes why its not rolling back the changes made by the querys from query2 to query7.

I had intensionally made a syntactical mistake in query8 for an exception.

Please guide me friends in this issue and plaease lent me know my mistakes in code.

Thank You!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:40:40+00:00Added an answer on June 2, 2026 at 1:40 pm

    Here’s how I would do it:

    Connection con = null;
    boolean ok = false;
    try {
        con = DriverManager.getConnection(...);
        ...
        con.commit();
        ok = true;
    } catch (...) {
        // diagnose exception 
    } 
    ...
    } finally {
        if (con != null) {
            try {
                if (!ok) con.rollback();
            } finally {
               con.close();
            }
        }
    }
    

    In other words, do the connection close and rollback in the finally block … and don’t repeat the code.

    And DON”T catch Exception … see below.


    A comment on the Question says this:

    As a general principle, catch Throwable, not just Exception. In fact, catch only Throwable, no need to repeat the blocks. That may even solve your problem.

    Catching Exception and especially Throwable is a bad idea UNLESS the next action after handling the exception is to exit the application. There are any number of potential unchecked exceptions / errors that might occur. You’ve no way of knowing what the cause of the unexpected exception was, or whether the application can safely recover.


    But the problem in my code is that its not rolling back the transactions done by the queries from query2 to query7

    Perhaps it is because some of those statements are non-transactional (e.g. CREATE TABLE), and executing a non-transactional statement causes the current transaction to commit automatically.

    Perhaps it is a problem with the old version of MySQL and the JDBC drivers that you are using. The “org.gjt.mm.mysql” driver is REALLY old, and I know for a fact that early versions of MySQL did not support transactions at all.


    As I suspected, you can’t rollback a CREATE TABLE in MySQL.

    Source:

    • StackOverflow Q&A Is it possible to roll back CREATE TABLE and ALTER TABLE statements in major SQL databases?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using JDBC + MySQL. I have several 1/0 values which I
I have JDBC code which inserts into a database table by executing a PreparedStatement.
I have this code for login validation using a Struts2 action class which calls
I have JDBC driver dependencies (e.g. h2, mysql ..) which have to be copied
I have a java-application using JDBC for database interaction. I want to do a
Currently i'm writing a JDBC application to manage a MySQL database. I have the
I have DAO code which contains some JDBC with Oracle-specific syntax, for example: select
I have the following code which calculates the median of a group of numbers
I am using Connector/J 5.1.10 as the JDBC driver for my Database application (which
Am using MySQL 5 on OS X - Snow Leopard... Have working code in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.