2

I tried to execute this code in order to generate random data in Oracle table:

DriverManager.registerDriver(new oracle.jdbc.OracleDriver());

        // Connect to the database
        Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "admin", "qwerty");

        PreparedStatement pstmt
            = conn.prepareStatement("BEGIN "
                + "FOR loop_counter IN 1..10 "
                + "LOOP "
                + "INSERT INTO EVENTS (EVENTID, SOURCE, TYPE, EVENT_DATE, DESCRIPTION) "
                + "VALUES (loop_counter, loop_counter, 'warning', "
                + "TO_DATE(TRUNC(DBMS_RANDOM.VALUE(TO_CHAR(DATE '2000-01-01','J') ,TO_CHAR(DATE '9999-12-31','J'))),'J'), "
                + "DBMS_RANDOM.string('x',15)); "
                + "END LOOP; "
                + "COMMIT; "
                + "END; ");

        pstmt.execute();
        pstmt.close();
        conn.close();

This code is successfully executed in PL/SQL deloper but for some reason it hangs in JUnit test execution. Do you have any idea why?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • 1
    What do you mean by *it hangs*? – Manu Nov 23 '15 at 08:38
  • JUint is not executed. I just see output message test started. – Peter Penzov Nov 23 '15 at 08:48
  • Try to find out exactly which statement hangs. You could add a `System.out.println` or logging after each statement to find that out. – SantiBailors Nov 23 '15 at 09:11
  • Also, you could store that SQL code in a variable, have it printed out or logged, and execute that output on your db, just to make sure the SQL executed by your Java code is exactly the same. – SantiBailors Nov 23 '15 at 09:16
  • Is that code inside a single loop? Creating connections in a loop is pretty heavy to start with. – aksappy Nov 23 '15 at 09:26
  • Is there a unique index on `EVENTS.EVENTID`? If so, is it possible that your test is being blocked by another session that has inserted or updated the same records but not yet committed? – Matthew McPeak Nov 24 '15 at 21:46

1 Answers1

0

It would be simpler if you did your looping in Java and performed a batch insert. This existing answer should help you:

Efficient way to do batch INSERTS with JDBC

Community
  • 1
  • 1
Andrew R. Freed
  • 357
  • 1
  • 10