I insert data to database by using Thread. the thread will not stop

I have a program of SDE for Netbeams.

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 primaryLey.

My program will insert the data by using 1000 Threads.


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 code is in 36 of Main class), 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(the code is in 38 of Main class),

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( the code is in 101 of Main.class)


session.flush();

However, I have no ideal to solve this problem.
TraceData.rar

Dear Frederic,

Session.flush() will trigger write all unsaved objects to database. Please use the session.close() to close the section when exception was throw.

Best regards,
Rain Wong