-1

I'm coding Java application that decode TCAP frame which will be reading from a text file, then insert decoded data in a database(Oracle)! So, at the beginning decoding and integration are perfectly performed, but when it reachs a finite of number decoded and inserted data, it starts triggering this error at the thread that assumes the insertion in the database:

   " java.lang.OutOfMemoryError: unable to create new native thread "
    " Exception in thread "Thread-465" java.lang.NullPointerException "

Code extract:

public void run(){ 
    Conn_BD connexion=new Conn_BD("thin:@localhost:1521:XE", "SYSTEM", "SYSTEM");     
    java.sql.Connection cn=connexion.connect(); 
    try { 
        Statement instruction = cn.createStatement(); 
        instruction.executeUpdate("update tcapBegin set "+ 
            trame+"='"+trame_val+"' where "+message+" like '"+trameId+"'"); 
        cn.close(); 
    } catch(SQLException e) { 
        System.out.print(e); 
    }
} 

Does anyone have an idea to resolve this problem?

Fritz
  • 9,987
  • 4
  • 30
  • 49
  • 1
    any code you can paste? – Julien May Mar 13 '13 at 13:58
  • Code would be helpful, but the two error messages seem to indicate that you are creating too many threads and a null pointer (maybe because of the first error). – Thilo Mar 13 '13 at 13:58
  • 11
    Your problem seems to be the `OutOfMemoryError` not the `NullPointerException`. – Kai Mar 13 '13 at 13:59
  • We'll need to see some code. Can you show how the threads are created and finished? Are they blocking anywhere? – Grambot Mar 13 '13 at 13:59
  • public void run(){ Conn_BD connexion=new Conn_BD("thin:@localhost:1521:XE", "SYSTEM", "SYSTEM"); java.sql.Connection cn=connexion.connect(); try{ Statement instruction = cn.createStatement(); instruction.executeUpdate("update tcapBegin set "+trame+"='"+trame_val+"' where "+message+" like '"+trameId+"'"); cn.close(); } catch(SQLException e){ System.out.print(e); } } – user2165627 Mar 13 '13 at 14:00
  • 3
    @user2165627 please add that to the question and format it appropriately. – Kai Mar 13 '13 at 14:01
  • How many of this threads are you creating? – Kai Mar 13 '13 at 14:02
  • I understood the error, baut i didn't find how to resolve it!!!!! it's evident that there are many Thread created, out of memory! but how we can manage this issue in Java! have any idea? – user2165627 Mar 13 '13 at 14:03
  • 1
    This is the run method of the thread, can you show us the code that instantiate the thread? are you pooling your thread or creating them again and again in a loop? – Boaz Mar 13 '13 at 14:05
  • 1
    @user2165627 are you starting a thread for EVERY query you do? – Fritz Mar 13 '13 at 14:05
  • I think that for every data that will be inserted on Thread will be created, i have more than 23 000 000 frames, and for every frame more than 20 data to decode and insert! – user2165627 Mar 13 '13 at 14:06
  • Yes! for every insert, a new Thrad will be instanciated @Gamb – user2165627 Mar 13 '13 at 14:08
  • 2
    @user2165627 and that is exactly your problem – Boaz Mar 13 '13 at 14:09
  • @user2165627 Then there you have your problem. If you're managing threads like this you'll have to pool them, otherwise think about all the threads that can be started through your app. – Fritz Mar 13 '13 at 14:10
  • I 'm creating a thread for every insert, and i have for every frame more than 10 data to insert! and i have more than 23 000 000 frame! – user2165627 Mar 13 '13 at 14:10
  • 1
    @user2165627 There are many approaches you can take here, you can have a group of threads in a pool you can manage to use when they're not currently busy with a query, you can also use a defined set of threads, queue a number of operations and let the thread handle them or you can use JPA. – Fritz Mar 13 '13 at 14:13
  • But i don't have an other choice!! the manner wich i fund to deal with the problem of decoding and integration in a database have to use a thread for every data decode to insert it in the database!! My question is, we haven't a soution that we can manger the number of threads, my be extends it or stop one thread and reuse for an other time? this doesnt exist? – user2165627 Mar 13 '13 at 14:15

2 Answers2

2

Instead of instantiating a thread per insert (or whatever other action you do), try and create a queue of "tasks", each task will represent an insert such thread should perform. When you have such a queue, you need to have a thread that "pushes" tasks into the queue and threads that perform the actual tasks by "pulling" them out of the queue and performing them. By working this way you won't need a thread per task but instead you'll be able to use a small set of general purpose threads that will take a task from the queue, execute it, and return to the queue for more work.

p.s. when you reuse your thread, don't create a connection in the run method, you don't have to recreate the connection each time.

Read about Executors and Thread Pooling
See Producer Consumer
See DB Connection pooling

Community
  • 1
  • 1
Boaz
  • 4,549
  • 2
  • 27
  • 40
0

You have this statement at the beginning of trhead

Conn_BD connexion=new Conn_BD("thin:@localhost:1521:XE", "SYSTEM", "SYSTEM"); 

Seems like you are creating a new connection everytime a new thread is created. Creating a connetion and then executing statement takes time so by the time your first connection gets closed so many other connections have been created that you can not create any more.

a better option will be if you use one static reference for connection.

private static Conn_BD connexion=new Conn_BD("thin:@localhost:1521:XE", "SYSTEM", "SYSTEM");     
private static java.sql.Connection cn=connexion.connect();
public void run(){
Statement instruction = cn.createStatement();
//code here 
instruction.close();
} 

once all the threads are done executing close the connection.

Sudeep
  • 121
  • 5