Link to home
Start Free TrialLog in
Avatar of pcarrollnf
pcarrollnf

asked on

JBOSS and Transaction ID

We are writing some classed that will log some information to the database that we will use later to generate activity reports.  Within a given transaction, there may be more than one activity occurring.  For example, if you download a file, you must first access the file to download it.  We would like to be able to link two activities (access and download) to the same transaction.  Is there a way in JBoss or through the java sun core api's to obtain the transaction id so that we can log it along with the activity?
Avatar of vzilka
vzilka

How are you trying to do it? By writing code inside your application or trying something external?

The basic thing to remember here is that the same execution thread runs your transaction (assuming your transaction does not span across machines, and that you remember that MDBs don't run in the same transaction as the code that sent the JMS message). If you have multiple transactions created by the same user request (do not do this, you get a major performance hit), they will all run by the same execution thread.

You can get the Thread id by using the command Thread.currentThread().

Now, either you log this in your application by hand, or you use some other ways, like:
1. JBoss 4.0 AOP.
2. Aspect/J
3. Write your own class loading mechanism.

I don't recommend any of them. The first two are based on AOP, which is a fairly new technology. The third requires enormous amounts of code to write. Also, the first and third solutions make your code totally unmigratable to other application servers.

So, to summarize, use Thread.currentThread(). It will print the thread unique ID.
One last thing - the same thread can be reused after the user request ends, so make sure you write to the log when the user request is finished.
Avatar of pcarrollnf

ASKER

I want to pass the transaction id to the JMS topic, along with some other information, and then let it do its own thing.  I have control of what activities are logged.  If I can get the transaction id, I will only log it with certain activities.  I was hoping that JBOSS may have a method implemented where you could easily obtain the transaction id or that I could easily obtain it by using the javax.transaction api's.  If neither can be done, then it is probably not worth the extra effort at this point in the project.  If anyone knows how to obtain that transaction ID, I'd appreciate the info.

I tried the Thread.currentThread() but this did not give me useful information.
What information do you need? a unique id in the system?
I need the unique ID for the transaction.  I assume that JBOSS may need a transaction ID to keep track of the transaction itself.  I was hoping that I could obtain that transaction id if it exists.
ASKER CERTIFIED SOLUTION
Avatar of vzilka
vzilka

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial