1

Server receives http request in servlet, from servlet calls method in ejb component.

public void ejbMethodVariant1(...) {
    //calling stored proc
    ...
    //calling same stored proc
}

public void ejbMethodVariant2(...) {
    //calling stored proc
    ...
    Thread t = new Thread(() -> {
             //calling same stored proc
        });
    t.start();
    try {
        t.join();
    } catch (InterruptedException e){
        ...
    }
}

Stored proc is always the same. "Calling stored proc" means:

  1. Getting connection from data source
  2. Creating callable statement
  3. Executing callable statement
  4. Closing statement
  5. Closing connection

In variant 1 - all works perfectly, without errors. Connections in first and second call have autoCommit=false.

In variant 2 - first call completes successfully, second - time out after 2 minutes (com.microsoft.sqlserver.jdbc.SQLServerException: The query has timed out.). Connection in first call has autoCommit=false, in second call have autoCommit=true.

Rémi Bantos
  • 1,899
  • 14
  • 27
Rustam
  • 1,397
  • 1
  • 13
  • 17
  • 1
    One remark, according to JEE spec, thread creation and management is not allowed in ejb. See [this answer](http://stackoverflow.com/questions/3816286/multithreading-in-a-stateless-session-bean#3816621) or [this](http://www.oracle.com/technetwork/java/restrictions-142267.html#threads) – Rémi Bantos Nov 07 '16 at 17:15

1 Answers1

2

You're starting a new thread which doesn't have the transaction context, security context, etc copied to it. If you want to use a new thread to run the statement consider using the EE Concurrency utilities in Java EE 7.

James R. Perkins
  • 16,800
  • 44
  • 60