This is a very simple example demonstrating a method to achieve a reliable lock in a cluster when the cluster shares a database connection. This example uses an Oracle database which has the following table/row:
<pre lang="sql">
create table locked(shortname varchar2(100) primary key, info varchar2(100));
insert into locked values('TestLock', '');
commit;
<pre lang="java">
package com.mccsoft.diapason.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestLock implements Runnable{
static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
static final String DB_URL = "jdbc:oracle:thin:@localhost:1521/XE";
static final String USER = "xxxx";
static final String PASS = "****";
public static void main(String[] args) {
for(int i = 0; i " + workTime);
Thread.sleep(workTime);
System.out.println("Releasing: " + threadName);
conn.commit();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(conn!=null)conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
}
}
Some links:
[http://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_lock.htm#i997212](http://docs.oracle.com/cd/E11882_01/server.112/e41084/ap_locks001.htm#SQLRF55502http://docs.oracle.com/cd/E11882_01/server.112/e41084/ap_locks001.htm#SQLRF55502
<a href=>)