public static final String DatabaseFile =
"/config/database.xml";
public static final String
MappingFile = "/config/mapping.xml";
public static final String Usage = "Usage: example
jdo";
private static Mapping _mapping;
private static JDO
_jdo;
static boolean bStart=false;
static long id=1;
public static void main( String[] args ) throws
Exception
{
new
Test();
// new
Test();
// new
Test();
}
public Test()
throws Exception
{
try
{
//
Load the mapping file
_mapping =
new Mapping( getClass().getClassLoader()
);
_mapping.loadMapping(
getClass().getResource( MappingFile ) );
_jdo = new
JDO();
_jdo.setConfiguration(
getClass().getResource( DatabaseFile ).toString()
);
_jdo.setDatabaseName(
"mqtest" );
OurThread th1 = new
OurThread();
OurThread th2 = new
OurThread();
OurThread th3 = new
OurThread();
OurThread th4 = new
OurThread();
OurThread th5 = new
OurThread();
do{
bStart=false;
Thread.yield();
}while(th1.bReached==false
|| th2.bReached==false || th3.bReached==false || th4.bReached==false ||
th5.bReached==false );
bStart=true;
th1.join();
th2.join();
th3.join();
th4.join();
th5.join();
}
catch
(Exception
e)
{
System.out.println("Exception
Main"+e.getMessage());
}
}
public Random rand=new Random();
class OurThread extends
Thread
{
public boolean
bReached=false;
public
OurThread()
{
bReached=false;
start();
}
public
void run()
{
Database
db1=null;
try
{
db1
=
_jdo.getDatabase();
db1.begin();
long
did=++id;
Doctor
doc=(Doctor)db1.load(Doctor.class,new
Long(did),Database.Shared);
System.out.println("Loaded
"+doc.getUserName());
//db1.lock(doc);
rand.setSeed(System.currentTimeMillis());
doc.setUserName("Doc"+did+"
("+(Math.abs(rand.nextInt())%1000)+")");
String
sName=doc.getUserName();
bReached=true;
while
(!bStart) Thread.yield();
System.out.println("Committing
"+Thread.currentThread());
db1.commit();
db1.close();
System.out.println("Successfully
updated "+sName);
}
catch (Exception
e)
{
System.out.println("Exception
Thread
"+e.getMessage());
//e.printStackTrace();
}
finally
{
try{db1.close();}catch(Exception
e){}
}
}
}
}
DOCTOR.JAVA
import java.util.*;
public class Doctor
extends User{
private Department dept =
null;
private String userName =
null;
public String getUserName()
{
return userName;
}
public void setUserName(String
aUserName) {
userName = aUserName;
}
public Department getDept()
{
return dept;
}
public void setDept(Department aDept)
{
if (dept!=aDept) dept = aDept;
}
}
DEPARTMENT.JAVA
import java.util.*;
public class Department
extends BaseJDO{
private String deptName =
null;
private ArrayList doctors = new ArrayList();
public String getDeptName()
{
return deptName;
}
public void setDeptName(String
aDeptName) {
deptName = aDeptName;
}
public ArrayList getDoctors() {
return
doctors;
}
public void addDoctor(Doctor aDoctor) {
if
(aDoctor!=null &&
!doctors.contains(aDoctor))
{
doctors.add(aDoctor);
aDoctor.setDept(this);
}
}
}
THIS IS THE OUTPUT
Loaded Doc5
(916)
�
Loaded Doc4 (393)
Loaded Doc6 (601)
Loaded Doc 3
Loaded Doc
2
Committing Thread[Thread-2,5,main]
Committing
Thread[Thread-1,5,main]
Committing Thread[Thread-4,5,main]
Committing
Thread[Thread-5,5,main]
Committing Thread[Thread-3,5,main]
Exception
Thread Nested error: org.exolab.castor.jdo.LockNotGrantedException:
persist.deadlock :(org.exolab.castor.jdo.engine.TransactionContextImpl@9ce060)
is having readlock on (Doctor/2/0 R/-) and is currently waiting for
(Doctor/5/4 R/-) whose readlock is owned by
(org.exolab.castor.jdo.engine.TransactionContextImpl@4ecfdd)
Exception
Thread Nested error: org.exolab.castor.jdo.LockNotGrantedException:
persist.deadlock :(org.exolab.castor.jdo.engine.TransactionContextImpl@9ce060)
is having readlock on (Doctor/6/1 R/-) and is currently waiting for
(Doctor/5/4 R/-) whose readlock is owned by
(org.exolab.castor.jdo.engine.TransactionContextImpl@476128)
Exception
Thread Nested error: org.exolab.castor.jdo.LockNotGrantedException:
persist.deadlock :org.exolab.castor.jdo.engine.TransactionContextImpl@9ce060)
is
having readlock on (Doctor/3/3 R/-) and is currently waiting for
(Doctor/5/4 R/-) whose readlock is owned by
(org.exolab.castor.jdo.engine.TransactionContextImpl@8bf072)
Exception
Thread Nested error: org.exolab.castor.jdo.LockNotGrantedException:
persist.deadlock :(org.exolab.castor.jdo.engine.TransactionContextImpl@9ce060)
is
having readlock on (Doctor/4/2 R/-) and is currently waiting for
(Doctor/5/4 R/-) whose readlock is owned by
(org.exolab.castor.jdo.engine.TransactionContextImpl@3b8b49)
Successfully
updated Doc5 (539)
We are in urgent requirement of a solution
and the we are not in a position to set cache-type to none due to performance
issues.