Article ID: 3336, created on Nov 30, 2007, last review on May 6, 2014

  • Applies to:
  • H-Sphere

Resolution

There are two possible types of connections to the H-Sphere database:

IMPORTANT:
In any case, you should use only prepared stataments when working with the H-Sphere database!

 

Regular Shared Connections

The part of the code which establishes and uses a regular database connection should look something similar to this:

        PreparedStatement ps = null;
        Connection con = Session.getDb();
        try {
            ps = con.prepareStatement(...........);
            ......................................
        } finally {
            Session.closeStatement(ps);
            con.close();
        }
        

 

Connections With Opened Transaction

In general, the part of the code which establishes and uses a transaction connection should look as follows:

        // checking if the transaction exists
        boolean wasTrans = Session.isTransConnection();
           Connection con = wasTrans ? Session.getDb() : Session.getTransConnection();
           try {
           // some operation with the database
           } catch (Exception ex){
        if (!wasTrans) {
        // rollback transaction
        con.rollback();
        // here should also be some data to release cache and to synchronize with old data in HS DB
        }
        throw ex;
           } finally {
        if (!wasTrans) {
        // commit transaction and release connection
           Session.commitTransConnection(con);
        } else {
           con.close();
        }
           }
        

Important:

  1. There is the pool of transaction connections (5 by default). Opening a new transaction that exceeds this maximum would cause the system to hang up.

  2. As the number of available transactions is resctricted, you must release the transaction connection you have opened by using the following command:

            Session.commitTransConnection(con);
            
  3. To get an opened transaction connection:

    
            con = Session.getTransConnection();
            

    Later in the code, you may get the same connection:

            con = Session.getDb();
            

    Never close a transaction connection you haven't opened!

  4. Please make sure you synchronize your objects with the corresponding data in the database. You must release your cache if you make a rollback.

f213b9fa8759d57bee5d547445806fe7 6311ae17c1ee52b36e68aaf4ad066387

Email subscription for changes to this article
Save as PDF