Transactional tasks

Now, in the following code, we can see a managed executor service that works with a transactional runnable object:

public class MyTaskWithTransaction implements Runnable {
private int id;
@Inject
private MyTransactionScopedBean bean;
...
@Override
@Transactional
public void run() {
try {
foundTransactionScopedBean = bean.isInTx();
} catch (Exception e) {
e.printStackTrace();
}
}
}

As seen in the previous sections, we can use a @Transactional annotation to define a transactional method. This can be done on the threads as well. This class injects the following managed bean:

@TransactionScoped
public class MyTransactionScopedBean implements Serializable {
public boolean isInTx() {
return true;
}
}

The @TransactionScoped annotation is a new normal scope class introduced in JTA 1.2 and indicates the annotated object to work only inside a transaction. Now, you can try to execute this code:

@Inject
private MyTaskWithTransaction taskWithTransaction;
...
defaultExecutor.submit(taskWithTransaction);

After we use the submit method, we will get the foundTransactionScopedBean field to true.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.113.197