some problem puzzled me

I am a newbie for EJB, and I have some problems about it .

1: I know MDB is used to consume jms message. and it can rollback a message when using ctx.setRollBackOnly() method. when do this, jms provider will resend this message. And I still know , when in ejb's session Bean, setRollBackOnly method can rollback a transaction(common in database). So , I had thought, when I do some database operation in MDB, and then setRollBackOnly(in CMT mode), database transaction will be rolled back and message will be sent again, I tried, but it seems not happen. so are these two transactions(jms transaction and database transaction) same to user?

2: In MDB or Session Bean, I can get ctx to do rollback or get time service, but when program run in my own method, I can't get ctx(unless it was passed as parameter of my own method). how Can I get this ctx?

I think it isn't a good manner to pass ctx everywhere in my own method.

thanks in advance for your reply.

[992 byte] By [dsecondymaila] at [2007-10-3 3:56:35]
# 1

Which implementation are you using? If the JMS MDB has container-managed transactions and TX_REQUIRED , rolling back the transaction must result in at least one message redelivery.

Regarding access to the EJBContext, in EJB 2.1 and earlier there is no way for utility code to retrieve it from the naming service.In EJB 3.0, you can define a component dependency on the SessionContext or MessageDrivenContext and look it up via java:comp/env. E.g.

@Stateless

public class FooBean implements Foo {

@Resource(name="ses_context_ref")

private SessionContext sessionCtx;

public void foo() {

Bar b = new Bar();

b.bar();

}

}

public class Bar {

public void bar() {

try {

InitialContext ic = new InitialContext();

// note that the name relative to java:comp/env matches the name() attribute of the

// @Resource annotation in FooBean

SessionContext sc = (SessionContext) ic.lookup("java:comp/env/ses_context_ref");

...

}

}

ksaksa at 2007-7-14 21:54:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

thanks for your detailed reply.

I'm demanded to use ejb 2.1 now. so I want to understand more detailed information about it.

I Read book masteringejb. and it says," one can rollback transaction by call ctx's setRollBackOnly method when using CMT" , can I use this in BMT? or When I configured CMT mode, Can I find UserTransaction through JNDI, and do rollback from that? because in my opinion , I think whenever I use CMT or BMT, container use UserTransaction control transaction internally. so if CMT was configured, I can still call UserTransaction to commit or rollback, although it should be controller by container. Am I right?

And I have another question, I 'm now using hibernate in container(JBoss), and hibernate provide Transaction class to control transaction, when hibernate was configured using JTA, what's the relation between Transaction from HIbernate and UserTransaction from Container? how do I use it both in CMT and BMT?

I found it's so difficult to study EJB. I read some books about it. but none detailed described these concept. could anyone recommends me some classic books?

thanks

happy365a at 2007-7-14 21:54:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

No, EJB does not permit the use of UserTransaction within a CMT bean.If you have CMT, you should use EJBContext.setRollbackOnly().If you use BMT for a JMS Message Driven bean, the message delivery will not be considered part of the the user transaction.

If you use a resource-manager specific API to control transactions, you are not using global JTA transactions.It's better to configure the resource to support JTA and then use the JTA API ( javax.transaction.UserTransaction) or EJB CMT to get global transactions.

You might want to try Head First EJB by Kathy Sierra and Bert Bates.

--ken

ksaksa at 2007-7-14 21:54:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

thanks for your reply and your book recommendation.

I feel so strange for BMT and MDB's behavior , I can rollback transaction and message in MDB just because it is configured CMT, but I can't do this in BMT,it's weird, I can't understand this. so the Transaction from CMT isn't the same as that from BMT, but from my opinion, this two thing should be same, it differs only the manner we use it , CMT use xml declarative manner to configure outside program, container manager and call JTA api for me, and BMT let me controll JTA api own, I have to write programatic code to control transaction.

So, thank you to mention this differences, I will write down this to note.

but I 'm not quite agree with one thing you said, "when using resouce manager specific API , then one are not using JTA".

I ever looked up hibernate's manual book from hibernate.org, and it says " if you use programatic transaction demarcation (e.g. in pure J2SE or with JTA/UserTransaction/BMT), you are adviced to use the Hibernate Transaction API to hide the underlying transaction system from your code"

So , as results , I think Hibernate's Transaction will call underlying JTA to finish its function(when it is configured to use JTA).

so , the ultimate conclusion is: Hibernate's Transaction use JTA to controll Transaction when it is configured to use JTA. and JTA make use of underlying resource manager to really commit or rollback (two phase commit some time) to resources(like databases). So when applying to Hibernate, I think , when I configured hibernate in Container, if configured in CMT, then I can't use Transaction and BMT's UserTransaction as you had said. when I configured hibernate with BMT and JTA, I can use UserTransaction and Hibernate's Transaction. internally , program 's flowchar likes: Transaction(Hibernate) > UserTransaction(JTA) -> two phase commit -> Session.commit or session.rollback(Hibernate)-> Underlying Connection's Comit or rollback. is it right?

But I still don't know, what's the result if I configure hibernate in Container(Jboss) with CMT or BMT, meanwhile, I don't configure Hibernate to use JTA, I configured it with direct jdbc connection's transaction, so what's the result and flowchart? or this will result in exception when deploying? because I think CMT and BMT must work together with JTA.

the book you recommended haven't been published in my local area, I will buy it as soon as possible, thank you.

happy365a at 2007-7-14 21:54:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

I haven't used Hibernate. Their recommendation makes sense for J2SE usage, but I don't agree with the recommendation regarding use within J2EE. Assuming hibernate's underlying resource is capable of acting as a JTA resource, it's better to use the standard J2EE programming model for dealing with global transactions.Otherwise, what happens when you later decide to interact with another kind of resource manager in the same global transaction? If you code to the Hibernate-specific API you'll need to change much more code to ensure that all the work is performed in the same transaction.If you've already coded to CMT or BMT any work done through the 2nd resource manager will automatically be included in the same global transaction.

In addition, most J2EE implementations are smart enough to not use an actual JTA transaction unless there are multiple resource managers in the transaction. So, you shouldn't see any real performance loss even if most of the time you only interact with hibernate in a given transaction.

--ken

ksaksa at 2007-7-14 21:54:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...