Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2004
    Posts
    9

    Unanswered: JDBC transaction setting

    hello people..
    I wrote a java code to know the transaction process. I clearly set the
    AutoCommit into false. Then I forcibly disable the commit just for checking purpose.
    But the database changes whenevewr the statement executes. I have given below the code.
    It should not update the database .But it does.I dont know where I am missing the technical things..
    Please Help me...



    import java.sql.*;
    import java.util.*;


    public class tranc
    {
    public static void main(String a[])
    {
    try
    {

    Class.forName("org.gjt.mm.mysql.Driver");
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/tech?user=root&password=");

    con.setAutoCommit(false);


    Statement st1=con.createStatement();
    st1.executeUpdate("update topic_details set topic='ram' where tid=11");


    Statement st2=con.createStatement();
    st2.executeUpdate("update topic_details set topic='ram' where tid=9");




    if(false)
    {
    con.commit();
    System.out.println("success");
    }else
    System.out.println("fail");


    } catch(Exception e){ e.printStackTrace();}



    }
    }

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    I think you should explicitly issue Connection.rollback() to revert the changes - otherwise I believe a COMMIT will be issued by the driver implicitly when you close the connection (which you do when the program terminates).

  3. #3
    Join Date
    Sep 2004
    Posts
    2
    Which version do you test on?

    MySQL is not really supporting transaction in 4.x or bellow, you should test it on oracle or sybase etc.

  4. #4
    Join Date
    Sep 2004
    Posts
    4

    PLease HeLP

    hi all, i am facing problem to connect to MYSQL database when i run i get this error message what is probably the causes ..

    Error:java.sql.SQLException: Unable to connect to any hosts due to exception: java.net.ConnectException: Connection refused

    ** BEGIN NESTED EXCEPTION **

    java.net.ConnectException
    MESSAGE: Connection refused

    STACKTRACE:

    java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl .java:305)
    at java.net.PlainSocketImpl.connectToAddress(PlainSoc ketImpl.java:171)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:158)
    at java.net.Socket.connect(Socket.java:452)
    at java.net.Socket.connect(Socket.java:402)
    at java.net.Socket.<init>(Socket.java:309)
    at java.net.Socket.<init>(Socket.java:124)
    at com.mysql.jdbc.StandardSocketFactory.connect(Stand ardSocketFactory.java:121)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:220)
    at com.mysql.jdbc.Connection.createNewIO(Connection.j ava:1768)
    at com.mysql.jdbc.Connection.<init>(Connection.java:4 40)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonReg isteringDriver.java:400)
    at java.sql.DriverManager.getConnection(DriverManager .java:512)
    at java.sql.DriverManager.getConnection(DriverManager .java:193)
    at tesDB.<init>(tesDB.java:23)
    at tesDB.main(tesDB.java:46)


    ** END NESTED EXCEPTION **

    this is my code :
    import java.net.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.sql.*;

    public class tesDB{
    private static Connection con;
    private static String Url;
    private static Statement stmt;
    private static ResultSet rs;
    public tesDB(String Url){
    try{
    JOptionPane.showMessageDialog(null,"Just Enter the try Block");
    this.Url =Url;
    JOptionPane.showMessageDialog(null,"Attempt to load Driver");



    // Class.forName("com.mysql.jdbc.Driver");
    DriverManager.registerDriver(new com.mysql.jdbc.Driver());
    JOptionPane.showMessageDialog(null,"Sucessfully Register DrIver");
    // the problem starts here ..
    con = DriverManager.getConnection(Url);
    JOptionPane.showMessageDialog(null,"Connection Established");
    stmt = con.createStatement();
    rs = stmt.executeQuery("Select * From Login");
    JOptionPane.showMessageDialog(null,"Create and Execute the statement");
    while(rs.next()){
    System.out.println(rs.getString(1));
    System.out.println(rs.getString(2));
    //to println

    System.out.println(rs.getString(3));
    }
    }catch(Exception e){
    System.out.println("Error:"+e);
    }
    }




    public static void main(String args[]){
    Url ="jdbc:mysql://localhost/jTest";

    tesDB tdb = new tesDB(Url);

    }

    }

  5. #5
    Join Date
    Jun 2004
    Posts
    9
    hi all

    I got the reason of mySQL transaction. mySQL provides some storage engines where some are not supporting transaction thing.So we have to change the storage engine to either INNODB or BDB so that we can do transaction operation.Defaultly mySQL creates the myISAM storage engine tables which are not supporting transaction. try this...& thankz for your support.
    Last edited by gilli; 10-04-04 at 09:32.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •