J2EE News, Forums, Discussions, Articles, Jobs, Books J2EEWorld.COM
J2EE News, Forums, Blogs, Articles, Books, Jobs
J2EE Careers | Vancouver J2EE Jobs | Sitemap

Search >> 
 Site last updated: Thursday, 24 July 2008
 Home arrow J2EE Weblog arrow Using Oracle Timestamps in Java
Main Menu
   Home
   J2EE News
   J2EE Weblog
   J2EE Books
   J2EE articles
   J2EE Web Links
   J2EE FORUM
   J2EE Download
   Newsletter
   About us
Latest News
Testing Portal Web Applications With HttpUnit
IBM vs Microsoft Developer Productivity Study
The J2EE 1.4 Tutorial
Linux file system structures for J2EE developers
Apache Jakarta DBCP Component -- efficient, feature-rich connection pool package
Login Form





Lost Password?
No account yet? Register
SoloSunglasses
SoloSunglasses: buy sunglasses direct from the manufacturer
Most Read Content
Using Oracle Timestamps in Java
IBM Restocks Eclipse Project
Introducing Sun Java Desktop System, Release 2
How to submit file to J2EE server?
Java 2 Enterprise Edition
Polls
What J2EE Application Server do you use?
 
Hit Counter
Visitors: 426103
Using Oracle Timestamps in Java
Written by Java boy   
Wednesday, 26 January 2005

Recently, I was doing a task would should avoid multiple transactions coming from different servers interfere each other. Since we also require to record the time of the task, I proposed using Oracle Timestamp type to keep the data integrity of multiple transactions.

Below table was introduced to keep the Server_status

SERVER_NAME LAST_PROCESS_TIME JOB_NAME
    

The pseudo SQL statement which ensures the data integrity of the hold back order transaction as below:

 

UPDATE SERVER_STATUS

SET LAST_PROCESS_TIME = LOCALTIMESTAMP AND

        SERVER_NAME= my_server_name

WHERE

        LOCALTIMESTAMP - last_process_time > 1 minute AND

        TASK_NAME =”my_task”

COMMIT

IF updated_record_number != 0 THEN 

//the above logic is to avoid too many overlapping transactions, which could possibly consuming DB resources

BEGIN TRANSACTION

         GET last_process_time

        //do my job here

         //below SQL script is to ensure any transactions that longer than 1 min will be failed if there’s another server already //executed the transaction successfully

        SELECT * from SERVER_STATUS FOR UPDATE of LAST_PROCESS_TIME nowait

        UPDATE SERVER_STATUS

SET LAST_PROCESS_TIME = LOCALTIMESTAMP AND

        SERVER_NAME= my_server_name

WHERE

        LAST_PROCESS_TIME = last_process_time    AND

        SYSDATE - last_process_time > 1 minute AND

        TASK_NAME =”Hold_back_order”

         IF updated_record_number == 0 THEN

                DO ROLLBACK

         ELSE

                DO COMMIT

END TRANSACTION

The executeUpdate() method will return the number of record set that has been updated. If the number is zero, which means another application server is already processing the transaction, then this server just ignore current transaction; otherwise it shall continue the process hold back order transaction.

some sample code as below for reference. Note, the compare of timestamp is a bit tricky.

 static String TIMESTAMP_FORMAT = "dd-mon-yyyy HH.MI.SSXFF PM";

con = DriverManager.getConnection(
       "jdbc:oracle:thin:@myserver:myport:dbname", USERNAME, PASSWORD);

Statement stmt = con.createStatement();

con.setAutoCommit(false);

   ResultSet rs = stmt.executeQuery("SELECT TO_CHAR(LAST_PROCESS_TIME, '" + TIMESTAMP_FORMAT + "') last_time from server_status");
   if (rs.next())
    last_time = rs.getString("LAST_TIME");
   for (int i=0; i<count; i++) {
    stmt.executeUpdate("UPDATE TEST SET TEST_VALUE=TEST_VALUE+1");
   }
   stmt.executeUpdate("select * from Server_status FOR UPDATE of LAST_PROCESS_TIME nowait");
  
   String sql= "update Server_status set LAST_PROCESS_TIME= LOCALTIMESTAMP " +
    "where LAST_PROCESS_TIME= TO_TIMESTAMP('" + last_time + "', '" + TIMESTAMP_FORMAT+"')";
   int ret = stmt.executeUpdate(sql);
  
   if (ret == 0) {
    con.rollback();
   } else {
    con.commit();

 

reference: http://www.psoug.org/reference/timestamp.html

 
< Prev   Next >
Who's Online

Warning: Invalid argument supplied for foreach() in /home/httpd/vhosts/j2eeworld.com/httpdocs/modules/mod_whosonline.php on line 32
latest topics
+ Free Porn Clips! 91120 FREE PORNO Movies!
+ pac man free video game 56.hi5.com free hostname movie pic p
+ Fuck the sperm
+ * * * FREE PORNO VIDEO * * * - company middlesex title
+ u0bbvr8x7c5ih1gouw86ywl75
+ u0bbvr8x7c5ih1gouw86ywl75
+ zfzigamaoa31tct06m3itz5czd4
+ zfzigamaoa31tct06m3itz5czd4
+ sex in art northern middlesex
+ arourioug
most download
J2Exe (4844)
J2TrayExe (1792)
J2WinService (1598)
HealthXP
HealthXP: Experience the Benefits of Health Innovations