java - Does new transaction starts when method b() is called from method a()? -



java - Does new transaction starts when method b() is called from method a()? -

does new transaction starts when method b() called method a()? or method phone call object , annotation not work? if so, how transaction started?

@stateless public class testbean { @transactionattribute(transactionattributetype.not_supported) public void a() { b(); } @transactionattribute(transactionattributetype.requires_new) public void b() { } }

you can check querying current transaction status using transactionsynchronizationregistry resource. here illustration tried on wildfly 8.1:

1 - inject transactionsynchronizationregistry resource:

@resource transactionsynchronizationregistry txreg;

2 - little helper query current tranaaction status , homecoming human readable string:

private string gettxstatus() { int txstatus = this.txreg.gettransactionstatus(); switch (txstatus) { case status.status_active: homecoming "status_active"; case status.status_committed: homecoming "status_committed"; case status.status_committing: homecoming "status_committing"; case status.status_marked_rollback: homecoming "status_marked_rollback"; case status.status_no_transaction: homecoming "status_no_transaction"; case status.status_prepared: homecoming "status_prepared"; case status.status_preparing: homecoming "status_preparing"; case status.status_rolledback: homecoming "status_rolledback"; case status.status_rolling_back: homecoming "status_rolling_back"; case status.status_unknown: homecoming "status_unknown"; default: homecoming "unknown(" + txstatus + ")"; }

3 - may instrument business methods quick , dirty logging (and never set system.out.println() in productive ejb code!

@transactionattribute(transactionattributetype.not_supported) public void a() { system.out.println("+++ a()"); system.out.println("tx status " + gettxstatus()); b(); system.out.println("tx status " + gettxstatus()); system.out.println("--- a()"); } @transactionattribute(transactionattributetype.requires_new) public void b() { system.out.println("+++ b()"); system.out.println("tx status " + gettxstatus()); system.out.println("--- b()"); }

4 - may invoke a() , b() client:

string beanname = "ejb:ejb3/mod3//sagmalwas!de.treufuss.ejb3project.client.helloworldremote"; helloworldbif bif = helloworldbif.class.cast(jndicontext.lookup(beanname)); bif.a(); bif.b();

which yields next output in servers console:

12:58:33,123 info [stdout] (ejb default - 3) +++ a() 12:58:33,123 info [stdout] (ejb default - 3) tx status status_no_transaction 12:58:33,123 info [stdout] (ejb default - 3) +++ b() 12:58:33,123 info [stdout] (ejb default - 3) tx status status_no_transaction 12:58:33,123 info [stdout] (ejb default - 3) --- b() 12:58:33,124 info [stdout] (ejb default - 3) tx status status_no_transaction 12:58:33,124 info [stdout] (ejb default - 3) --- a() 12:58:33,127 info [stdout] (ejb default - 4) +++ b() 12:58:33,128 info [stdout] (ejb default - 4) tx status status_active 12:58:33,128 info [stdout] (ejb default - 4) --- b()

which proves no transaction started invocation of b() a

addendum

you may forcefulness creation of new transaction invoking b() method via beans business interface ejb context. that...

1 - inject ejb context resource

@resource private sessioncontext ctx;

2 - invoke b() method via business interface:

@transactionattribute(transactionattributetype.not_supported) public void a() { system.out.println("+++ a()"); system.out.println("tx status " + gettxstatus()); // direct invocation treated pojmc (plain old java method phone call b(); // indirect invocation via ejb context helloworldlocal thisbean = ctx.getbusinessobject(helloworldlocal.class); thisbean.b(); system.out.println("tx status " + gettxstatus()); system.out.println("--- a()"); }

java java-ee transactions ejb

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -