JDBC commit() not working?

JDBC commit() not working?

Post by Robert Berg » Fri, 17 Aug 2001 12:24:59



I have some code that's doing connection pooling for some servlets. I
turn autoCommit off and let each caller determine when to commit.

Hoever, commit() does not seem to be releasing read locks, causing
my periodic VACUUM task to block.

In the following code, after commit() is called, ps shoes the backend
state as "idle in transaction". If I turn autocommit on, the state is
"idle" Why the difference?

import java.sql.*;

public class ctest {
    public static void main(String argv[]) {
        try {
            String driverName = "org.postgresql.Driver";
            Class.forName(driverName).newInstance();

            Connection con = DriverManager.getConnection(
                "jdbc:postgresql://127.0.0.1/jackson","db","zzzz");

            con.setAutoCommit(false);

            Statement stmt = con.createStatement();

            ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM s_image");
            rs.next();
            int count = rs.getInt(1);
            rs.close();

            con.commit();

            System.out.println(count);

            Thread.sleep(30000);

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

Quote:}

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate

message can get through to the mailing list cleanly
 
 
 

JDBC commit() not working?

Post by Barry Li » Fri, 17 Aug 2001 13:11:47


Robert,

VACUUM requires an exclusive lock on a table.  This exclusive lock
includes reads (i.e. selects) as well as writes.  Thus if you have
issued a SELECT statement and not committed after the select, the select
will lock the table and thus prevent the VACUUM from running.  (I know
this because I just went through my code cleaning up this same problem).

As for the difference between 'idle' and 'idle in transaction', if
autocommit if false, then when the commit() method is called it
automatically starts the next transaction with a begin (it essentially
does:  commit; begin;) thus 'idle in transaction' because a new
transaction is started.  With autocommit true, there are no explicit
transactions (i.e. begin calls), thus it is just in an 'idle' state.

thanks,
--Barry


> I have some code that's doing connection pooling for some servlets. I
> turn autoCommit off and let each caller determine when to commit.

> Hoever, commit() does not seem to be releasing read locks, causing
> my periodic VACUUM task to block.

> In the following code, after commit() is called, ps shoes the backend
> state as "idle in transaction". If I turn autocommit on, the state is
> "idle" Why the difference?

> import java.sql.*;

> public class ctest {
>     public static void main(String argv[]) {
>    try {
>        String driverName = "org.postgresql.Driver";
>        Class.forName(driverName).newInstance();

>        Connection con = DriverManager.getConnection(
>            "jdbc:postgresql://127.0.0.1/jackson","db","zzzz");

>        con.setAutoCommit(false);

>        Statement stmt = con.createStatement();

>        ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM s_image");
>        rs.next();
>        int count = rs.getInt(1);
>        rs.close();

>        con.commit();

>        System.out.println(count);

>        Thread.sleep(30000);

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

> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate

> message can get through to the mailing list cleanly

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate

message can get through to the mailing list cleanly

 
 
 

1. JDBC not working with tomcat 4 / works on command line

I've written a class like this:

import java.sql.*;

public class db {

     private Connection conn = null;

     public db() {
     }

     public Connection connect() {

             // return the connection if it's already set up
        //            if (conn != null) { return conn; }

             // check JDBC driver exists
             try {
                 Class.forName("org.gjt.mm.mysql.Driver").newInstance();
             }
             catch (Exception e) {
                   System.err.println("Unable to load JDBC driver " + e);
                   e.printStackTrace();
             }

             // connect to DB
             try {
                 // TODO: put connection params into conf file
          \       conn =
DriverManager.getConnection("jdbc:mysql:///scaleforum","arse","");

             }
             catch (Exception e) {
                 System.err.println("Unable to connect to DB " + e);
                 e.printStackTrace();
             }

            if (conn == null) { System.err.println("Connection is knackered"); }

            return conn;
         }

This works fine called from the commandline:

But fails miserably called in a JSP context from Tomcat 4 (the
connection returned is null, example below).

public class Arse {

   private db mydb = new db();
     private String subject = new String();

     // insert a post
     public String poo () {

        Connection conn = mydb.connect();

        if (conn == null) { return("connection is knackered"); }

Can anyone help, I'm going mad!

2. How does BDE access Paradox tables?

3. JDBC, Postgres and connection in not auto-commit mode

4. Progress Gurus Question...

5. DTS bulk insert, commit or rollback not working?

6. Configuring MS Search Engine or Full Text Indexing

7. JDBC, Postgres and connection in not auto-commit mode

8. When does COMMIT not commit?

9. ISQL Begin Work & Commit Work

10. begin works and commit works in storeprocs

11. ISQL Begin Work & Commit Work

12. begin works and commit works in storeprocs