My environment is:
JDK 6 update 17
Database : MS-SQL 2008.
Describe:
I have a database table, it has two fields. They are id and name. The id is primaryKey.
My program will insert the data by using 1000 Threads(Employee_Normal_Status and Employee_Error_Status ).
for (int i = 1; i <= 10; i++) {
//This Thread can run Complete
new Thread(new Employee_Normal_Status()).start();
//This Thread is not good.
new Thread(new Employee_Error_Status()).start();
}
When I just only run the code, the program will run correct and stop.
class Employee_Normal_Status implements Runnable {
public void run() {
try {
this.setEmployee();
} catch (PersistentException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void setEmployee() throws PersistentException, SQLException {
PersistentSession session = TraceDataPersistentManager.instance().getSession();
PersistentTransaction t = session.beginTransaction();
try {
Frederic frederic = new Frederic();
frederic.setId(new Main().getRandomnumber());
frederic.setName(new Main().getRandomnumber());
session.save(frederic);
t.commit();
} catch (Exception ex) {
t.rollback();
} finally {
session.flush();
}
}
}
But, when I run the code
I use the same data to create the error status.
the program can’t stop.
class Employee_Error_Status implements Runnable {
public void run() {
try {
this.setEmployee();
} catch (PersistentException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void setEmployee() throws PersistentException {
PersistentSession session = TraceDataPersistentManager.instance().getSession();
PersistentTransaction t = session.beginTransaction();
try {
Frederic frederic = new Frederic();
frederic.setId("9999");
frederic.setName("Frederic");
session.save(frederic);
t.commit();
} catch (Exception ex) {
t.rollback();
} finally {
session.flush();
}
}
}
Please tell me how to solve this status.
I find the problem is the Thread can’t through the code
session.flush();
However, I have no ideal to solve this problem.
Thans very much.