Documente Academic
Documente Profesional
Documente Cultură
Location: http://www.jguru.com/faq/Transactions
Ownership: http://www.jguru.com/misc/user-agree.jsp#ownership.
What is a transaction?
Location: http://www.jguru.com/faq/view.jsp?EID=2555
Created: Dec 14, 1999 Modified: 1999-12-14 12:34:25.051
Author: Jerry Smith (http://www.jguru.com/guru/viewbio.jsp?EID=9)
Transaction support is also important because systems can fail at any point in an
execution sequence. If, for example, a database update operation involves several
dependent tables, and the computer system fails when the database update is only
partially completed, the database could be left in an inconsistent, even inoperable,
state.
File and database processing were essential operations with early multiprocessing
computing systems; hence, transaction processing was necessary to guarantee their
integrity. In today's complex distributed environments, transactions are fundamental
to many distributed operations, for example, guaranteed delivery and ordering of a
series of messages exchanged between two distributed application components. In
this scenario, the message exchange should take place within an atomic execution
sequence.
Re: What is the difference between JTA and JTS? When must...
Author: kirupakaran jayaraman
(http://www.jguru.com/guru/viewbio.jsp?EID=387804), Mar 26, 2001
JTS- (Java Transaction service) It's a service offered by various APPLICATION
SERVERS as mentioned in J2EE specifications. It's simplpy the the in-built
support given to reduce the burden of programmers hardcoding the entire
transaction logics (related to low level problems like network problems) And JTA
- Java Transaction API is one such implementation of JTS specification prepared
by sun microsystems. This API includes 1. javax.transaction.Status 2.
javax.transaction.Transaction 3. javax.transaction.TransactionManager 4.
javax.transaction.UserTransaction 5. javax.transaction.xa.Xid 6.
javax.transaction.xa.XAResourse 7. javax.transaction.Synchronisation any queries
mail to kirupa_raj@rediff.com bye kirupa
Re: What is the difference between JTA and JTS? When must...
Author: bony boy (http://www.jguru.com/guru/viewbio.jsp?EID=408041), Apr 22,
2001
JTA is an API and JTS is an implementation(will be done by EJB Server vendors)
of the API .
Re[2]: What is the difference between JTA and JTS? When must...
Author: Krishma Singla
(http://www.jguru.com/guru/viewbio.jsp?EID=1183951), Jul 6, 2004
I find it contradictory to Kirupa
Re[2]: What is the difference between JTA and JTS? When must...
Author: sandeep kumar
(http://www.jguru.com/guru/viewbio.jsp?EID=1225718), Feb 7, 2005
The JavaTM2 Platform, Enterprise Edition (J2EE) simplifies application
programming for distributed transaction management. J2EE includes support
for distributed transactions through two specifications, Java Transaction API
(JTA) and Java Transaction Service (JTS). The JTA is a high level API that
allows applications and application servers to access transactions. The JTA
defines a high-level transaction managementspecification intended for
resource managers and transactional applications in distributed transaction
systems. The JTS specifies the implementation of a Transaction Manager,
which supports JTA and implements the Java mapping of the OMG
ObjectTransaction Service (OTS) 1.1 specification at the level below the API.
It is theJTS that is responsible for managing the interoperability of transactions
between platforms.
JTS vs MTS
Author: Hrishikesh Srivatsa (http://www.jguru.com/guru/viewbio.jsp?EID=742668),
Jan 31, 2002
Can we compare MTS and JTS ? JTS is a specification for the Transaction Manager
and will be a vendor specific implementation. J2EE speaks about flat (atomic)
transactions, can JTS support multiple transactions?
JTA vs JTS
Author: Guy Pardon (http://www.jguru.com/guru/viewbio.jsp?EID=1208077), Oct
29, 2004
JTA is the Java transaction API, JTS is the mapping of CORBA's OTS to Java.
JTS is a possible implementation of JTA, but there are other possibillities such as
this embeddable java transaction service from the transaction management
company. Guy
Yes. A JTS transaction manager implements the Java mappings of the CORBA OTS
1.1 specification, specifically, the Java packages org.omg.CosTransactions and
org.omg.CosTSPortability. A JTS transaction manager can propagate transaction
contexts to another JTS transaction manager via IIOP.
How you gain access to UserTransaction objects varies depending on the type of
client. Enterprise JavaBeans provides two types of transaction management:
In other environments, for example, a web server that supports servlets and/or
JavaServer Pages (JSP), a JMS server, and others, you obtain a UserTransaction
object via JNDI. Of course, for a servlet to obtain a UserTransaction object, there
must be a JTS-capable server to deliver the object.
Typically, the server provides the JNDI look-up name either directly or via a system
or server property. For example, with the WebLogic server, you would use a code
segment similar to the following:
...
Context c = new InitialContext();
UserTransaction ut = (UserTransaction)
c.lookup("javax.jts.UserTransaction");
ut.begin();
// perform multiple operations...
ut.commit()
...
With J2EE implementations, you obtain the UserTransaction object with a code
segment similar to the following:
...
Context c = new InitialContext();
UserTransaction ut = (UserTransaction)
c.lookup("java:comp/UserTransaction");
ut.begin();
// perform multiple operations...
ut.commit()
...
If the environment provides the UserTransaction object via a system property, you
would use a code segment similar to the following:
...
String transName = System.getProperty("jta.UserTransaction");
Context c = new InitialContext();
UserTransaction ut = (UserTransaction) c.lookup(transName);
ut.begin();
// perform multiple operations...
ut.commit()
...
JNDI remote look-up names and property names vary, of course, across
servers/environment.
WebSphere 5.0
Author: Christoph Ernst (http://www.jguru.com/guru/viewbio.jsp?EID=1106402),
Aug 6, 2003
In WebSphere 5.0 the JNDI-Lookup works like
UserTransaction ut = (UserTransaction)
ctx.lookup("jta/usertransaction");
See e.g. http://www-
3.ibm.com/software/webservers/appserv/doc/v30/ae/web/doc/ent_beans/atswpgec.htm
The link above is a bit outdated (from WAS 3.0 documentation). Check the current Infocenter for deta
support in WAS 5.0:
http://publib.boulder.ibm.com/infocenter/wasinfo/index.jsp?topic=/com.ibm.websphere.base.doc/info/
The lookup "jta/usertransaction" is still supported for J2EE apps with a spec level of 1.2 or lower. But
1.3 or later "java:comp/UserTransaction" is demanded.
Re[2]: WebSphere 5.0
Author: William Koscho
(http://www.jguru.com/guru/viewbio.jsp?EID=1148721), Feb 24, 2004
I am familiar with looking up the JNDI reference, but I am having trouble
defining the UserTransaction in Websphere. Can someone help me to set it up?
How do I create a javax.transaction.UserTransaction object and map it to
"java:comp/UserTransaction"? I am using WSAD 5.1 - any help is appreciated.
Thanks, Bill
From J2EE 1.3 specs: "The J2EE platform must provide an object
implementing the javax.transaction.UserTransaction interface to all web
components. The platform must publish the UserTransaction object in the
Java™ Naming and Directory Interface (JNDI) name space available to
web components under the name java:comp/UserTransaction."
using java:comp/UserTransaction
Author: Radhakrishnan Nariangadu
(http://www.jguru.com/guru/viewbio.jsp?EID=752623), Aug 4, 2005
Can I use the J2EE JNDI lookup even within a EJB container?
I'm developing a Hibernate based DB component that will provide a uniform
transactional interface anywhere,ie, J2EE container or normal JVM. Therefore within
a Bean managed Session bean, rather than obtaining UserTransaction from the
EJBContext will everything work smoothly if I obtain the UserTransaction using the
JNDI lookup?
...thanks in advance
Keep in mind that even if the JTS implementation supports nested transactions, this
transaction manager-level support does not guarantee support for nested
transactions in an application. For example, the EJB 1.1 specification does not
support nested transactions.
What are the ramifications of mixing JTA and non-JTA transactions where
the same resources are involved?
Location: http://www.jguru.com/faq/view.jsp?EID=2570
Created: Dec 14, 1999
Author: Jerry Smith (http://www.jguru.com/guru/viewbio.jsp?EID=9)
If mixed transactions are necessary (for whatever reason), the application should
manipulate the transactions in serial order, that is, start and finish the JTA
transaction before creating the non-JTA transaction (or vice versa). If, for example, a
JMS client creates a transaction via a UserTransaction and this transaction uses a
database resource via the transaction manager, there could be problems if the same
application directly accesses the database using a shared database connection.
There are many subtle and complex interactions between the use of
transactional
resources and threads. To ensure correct operation, web components
should obey
the following guidelines, and the web container must support at least
these usages.
• JTA transactions should be started and completed in the thread in
which the
service method is called. Additional threads that are created for any
purpose,
should not attempt to start JTA transactions.
No. A JTA transaction must start and finish within a single invocation (of the
service() method). Note that this question does not address servlets that maintain
and manipulate JDBC connections, including a connection's transaction handling.
How does a client application that employs, say, session beans, obtain a JTA
UserTransaction object from the EJB server?
Location: http://www.jguru.com/faq/view.jsp?EID=2573
Created: Dec 14, 1999
Author: Jerry Smith (http://www.jguru.com/guru/viewbio.jsp?EID=9)
It doesn't. Session beans may obtain a JTA UserTransaction object for method-level
bundling of business logic-related operations as a logical unit of work. If a client
application must employ JTA transactions, for whatever reason, doing so is
independent of the fact that it also uses enterprise beans (which implement their
own transaction management via the EJB server); hence, the client must obtain the
UserTransaction object in some other context, for example, from the J2EE
environment.
One possible example would be a scenario in which a client needs to employ two (or
more) session beans, where each session bean is deployed on a different EJB server
and each bean performs operations against external resources (for example, a
database) and/or is managing one or more entity beans. In this scenario, the client's
logic could required an all-or-nothing guarantee for the operations performed by the
session beans; hence, the session bean usage could be bundled together with a JTA
UserTransaction object.
In the previous scenario, however, the client application developer should address
the question of whether or not it would be better to encapsulate these operations in
yet another session bean, and allow the session bean to handle the transactions via
the EJB container. In general, lightweight clients are easier to maintain than
heavyweight clients. Also, EJB environments are ideally suited for transaction
management.
In some situations, it's necessary for a (stateful) session bean to selectively control
which methods participate in transactions, and then take over the bundling of
operations that form a logical unit of work.
Good explanation
Author: Santosh Malpekar (http://www.jguru.com/guru/viewbio.jsp?EID=30121),
Mar 30, 2000
Good explanation
Comments
Author: Suresh S (http://www.jguru.com/guru/viewbio.jsp?EID=320716), Apr 10,
2001
If some scenarios would have given, it would be better to understand. Thanks
Yes, but with restrictions. By definition, a stateless session bean has no state; hence,
each method invocation must be independent. (The bean can be "swapped out"
between method invocations.) Thus, a stateless session bean can obtain a
UserTransaction object via the EJBContext using the getUserTransaction()
method, but it must start and finish each transaction within the scope of a method
invocation.
How do you configure a session bean for bean-managed transactions?
Location: http://www.jguru.com/faq/view.jsp?EID=2581
Created: Dec 14, 1999 Modified: 2000-08-24 18:54:58.201
Author: Jerry Smith (http://www.jguru.com/guru/viewbio.jsp?EID=9)
Bean Managed
Author: Rajeev Sharma (http://www.jguru.com/guru/viewbio.jsp?EID=334960), Feb
23, 2001
Bean Managed
How do you configure the transaction characteristics for a session bean with
container-managed transactions?
Location: http://www.jguru.com/faq/view.jsp?EID=2582
Created: Dec 14, 1999 Modified: 2000-08-24 18:55:27.106
Author: Jerry Smith (http://www.jguru.com/guru/viewbio.jsp?EID=9)
Re: Set it to what? Can we have some sample code for the...
Author: Damodara Rao (http://www.jguru.com/guru/viewbio.jsp?EID=428922),
May 26, 2001
Within one transaction one row has to be inserted in oracle database throuh CMP
EntityBean and the visibility of that row has to be made for inserting some other
row in some other table because of constraints.How to make it visible within the
transaction?
Re[2]: Set it to what? Can we have some sample code for the...
Author: selva raju (http://www.jguru.com/guru/viewbio.jsp?EID=594229),
Dec 23, 2001
I am not how you have the Entity Bean Design for that.But I think the
following will be the Solution for you. Create one Method in Session Bean
with the transaction Attribute as "Required" and call the Both the Entity Beans
from that method.Here I have assumed that you two different entity beans to
represent the two different tables.And specity the transaction attribute for the
create method of the Entity Beans as "Supports" or "Required".
It doesn't. Entity beans do not employ JTA transactions; that is, entity beans always
employ declarative, container-managed transaction demarcation. Entity beans, by
definition, are somewhat tightly coupled (via the EJB container and server) to a
datastore; hence, the EJB container is in the best position to manage transaction
processing.
No. One of the motivations for using a distributed component architecture such as
Enterprise JavaBeans is to free the business logic programmer from the burdens that
arise in multiprogramming scenarios.
For example, suppose you have a bean that has a qty field and a method called
updateQuantity.
Then you could define updateQuantity in the bean class as
However this could lead to incorrect behavior because the container is free to create
several
instances of the bean to process method requests. Calls to these methods (on two
different beans) can occur
concurrently and that is the problem. So for example each instance gets the qty from
the database
and could start out with the same qty. When the qty is decreased in each of two calls
in two
transactions each bean would decrease it's qty and store it back to the database. Here
is a better picture,
This of course is the wrong answer. I should store -1 into the db.
There is a common misconception that since the container serializes calls to the bean
instance that calls to the bean object are serialized. Hence the programmer must
either serialize calls to the object or must use features of the database to handle the
above situation. Serializing calls to the entity and session objects does not happen
automatically. For session objects the container will throw an exception if the calls
happen concurrently. Even many companies that develop ejb servers hold these
misconceptions.
It depends on the server implementation. The JTA and JTS specifications define
client-, application-, and transaction manager-level operations. There is nothing to
prevent a Java application server from implementing, JMS, EJB, and JTS functionality.
On the other hand, it could (if available) obtain transaction manager services
indirectly from another server.
Under certain conditions, yes. In this particular case, the JMS server would have to
implement the appropriate resource-related interfaces in javax.transaction.xa so
that the JTS transaction manager can enlist the JMS server as an intermediary,
playing the role of a resource manager with respect to the JMS application.
• begin()
• commit()
• getStatus()
• rollback()
• setRollbackOnly()
• setTransactionTimeout()
The following interfaces prescribe services that are used by application servers, for
example, an EJB server, in communicating with and requesting services from a JTS
transaction manager:
• Synchronization
• Transaction
• TransactionManager
In general, there is no reason for a client to use these three interfaces. When an
application employs these services, it has, by definition, crossed the line from client
to application server. Middleware (application servers) exist for a reason, primarily to
serve as an intermediary between lightweight clients and dedicated servers, for
example, a database server. If a client needs the services of a JTS transaction
manager, and these services are not provided by an existing application server, the
client probably needs to be factored into a thin client(s) plus an application server.
That is, the distributed application needs to implement a custom application server
that insulates distributed clients from low-level transactional details.
Clients do not use this package. This package prescribes functionality that is
implemented by JTS transaction managers.
Clients do not use this package. This package prescribes functionality that is used by
JTS transaction managers when managing global transactions involving one or more
resources such as database servers, JMS servers, and others.
The JMS specification does not require that JMS implementations support distributed
transactions. If a JMS implementation supports distributed transactions, it's likely
that it will do so through the JTA API, providing a JTA UserTransaction object via
JNDI.
In order to ensure that a transaction can be rolled back, a software system typically
logs each operation, including the commit operation itself. A transaction/recovery
manager uses the log records to undo (and possibly redo) a partially completed
transaction.
Two-phase commit is a transaction protocol designed for the complications that arise
with distributed resource managers. With a two-phase commit protocol, the
distributed transaction manager employs a coordinator to manage the individual
resource managers.
• Phase 1
o Each participating resource manager coordinates local operations and
forces all log records out:
o If successful, respond "OK"
o If unsuccessful, either allow a time-out or respond "OOPS"
• Phase 2
o If all participants respond "OK":
Coordinator instructs participating resource managers to
"COMMIT"
Participants complete operation writing the log record for the
commit
o Otherwise:
Coordinator instructs participating resource managers to
"ROLLBACK"
Participants complete their respective local undos
In order for the scheme to work reliably, both the coordinator and the participating
resource managers independently must be able to guarantee proper completion,
including any necessary restart/redo operations. The algorithms for guaranteeing
success by handling failures at any stage are provided in advanced database texts.
The advanced-java mailing list is probably your best bet for finding help for
discussing transactions. As far as newsgroups go, I would guess that
comp.lang.java.corba is your best bet. You can probably have fun by posting to the
microsoft.public.microsoft.transaction.server.programming newsgroup.
Are there any good books about transactions (especially w.r.t. to Java)?
Location: http://www.jguru.com/faq/view.jsp?EID=23916
Created: Mar 13, 2000 Modified: 2000-03-14 07:19:19.789
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7) Question
originally posed by John Mitchell PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=4
The Programming with Enterprise JavaBeans, JTS, and OTS book from Andreas Vogel
and Madhavan Rangarao is rumored to be good, though it is approaching a year old.
The Java Enterprise in a Nutshell book provides API-level coverage, but if you don't
know it already, it won't help.
Comments and alternative answers
Transactions article
Author: Jerry Smith (http://www.jguru.com/guru/viewbio.jsp?EID=9), Apr 30, 2001
Although it's not a book, J2EE Transaction Frameworks is a good tutorial-type article.
My session beans call other bean methods within a transaction. Using bean-
managed transactions, how should I take care of commit and rollback ?
Location: http://www.jguru.com/faq/view.jsp?EID=62121
Created: Jun 2, 2000 Modified: 2000-09-12 15:55:36.874
Author: Siva Visveswaran (http://www.jguru.com/guru/viewbio.jsp?EID=46210)
Question originally posed by Shalabh Nigam
(http://www.jguru.com/guru/viewbio.jsp?EID=47481
1. Coding step:
Note you have to begin a txn before opening dB connections and close connections
before committing.
2. Deployment step:
- Your app server must support a JTS for distributed txns. Most do.
- Verify there is no conflict with the bean transaction properties in calling beans.
You can use an implementation of JTA that uses JTS. JTS is the Java Mapping of the
CORBA OTS (Object Transaction Service) that supports distributed transactions.
In EJB 1.1, the way your EJB accesses multiple databases is the same as for
accessing a single database: the datasource is defined in the deployment descriptor
and looked up by the EJB using the JNDI environment naming context, that is,
java:comp/env.
Warning: this functionality, accessing multiple databases in EJB within the same
transaction context, is not supported by all products.
By saying "how do I manage" it seems like you are doing bean-managed transaction
using the javax.transaction.UserTransaction interface to explicitly demarcate
the transaction boundaries. In EJB 1.1 you can do that with a session bean. (With
the upcoming 2.0 specification, you can also do it with message beans!)
In the case of a stateful session bean, the EJB specification allows the transaction
boundary to be maintained over multiple method calls i.e., the bean method is not
required to commit a transaction at the end of a business method (unlike stateless
session beans). You could therefore achieve the effect of a long transaction over the
entire use-case that the session bean implements. It is possible for the bean to open
and close database connections for each method instead of holding it over the entire
use-case. See the code example in the EJB 2.0 (Draft) Specification Section 16.4.3
Enterprise Beans Using Bean Managed Transaction Demarcation.
I think the author is not addressing the issue of "long" transactions. Instead, this
answer talks about bean-managed transactions across multiple method invocations of
a bean--this is easy to achieve by bracketing the method with a user transaction.
However, long transactions occur when one tries to implement business use cases
requiring human (or even other system) interaction. As long as the transaction context
can be maintained across multiple invocations, it is possible to maintain a "long"
transaction across the complete business use case.
Coming back to the question, the basic requirement is the ability to maintain the
transaction context across the use case. For instance, consider a web-based
implementation of a use case--the implementation consisting of several HTTP
requests with each request resulting in certain transactional resource access. In this
case, the transaction context is associated with the thread of execution of the request.
That is, the lifetime of the transaction context cannot be beyond the scope of the
request. In this model, it is not possible to implement long transactions as the
transaction context cannot be propagated across multiple requests. As far as the J2EE
server is concerned, each request involves a new transaction.
A possible alternative is to use Java application clients. In this case, as long as the
application client is alive, it is possible to maintain the user transaction object, and
therefore the transaction context is maintained across all the requests from the
application client to the beans.
1. It is true that a session bean can maintain data between multiple http
requests.
2. Say you have transaction A part of request A and transaction B part of
request B - both requests A and B being part of the same session - and
request A preceding request B.
3. One would assume that if transaction A was not committed - and then at
the end of transaction B - both A and B were committed - one would attain
the 'long' transaction effect.
However, at the end of request A, one has lost the transaction A context - and
hence transaction A cannot be committed at the end of transaction B - even though
the two belong to the 'same session'. The only way to attain something like the
above would be through nested transactions - and the J2EE platform only supports
flat transactions. A flat transaction cannot have any child (nested) transactions.
Where can I find the API documentation for the Java Transaction API (JTA)?
Location: http://www.jguru.com/faq/view.jsp?EID=134146
Created: Aug 24, 2000 Modified: 2000-08-24 11:16:10.042
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)
You can find the JTA documentation from Sun's J2EE API documentation page.
You say that your business requirements are to start a transaction at the beginning
of the session bean method and then to terminate it at the end of the session bean
method. Why don't you forget about starting the transaction in the servlet and let
CMT in the beans do their thing? Think of the session bean methods as "use-case"
methods. If they use entity beans, you'll usually set the methods to REQUIRES_NEW
and the entity bean methods to one that lets it be in the session bean context
(mandatory, required, supports).
Why is the session bean updating the database? Are you implementing
SessionSynchronization too? Usually, for the sake of transaction handling if you're
updating you'll use entities. If the session bean is just reading, then using database
data is okay (but you'll probably want to also implement SessionSynchronization).
2 Phase Commit
Author: prajakt samant (http://www.jguru.com/guru/viewbio.jsp?EID=272793), Mar
22, 2001
All the EJB Containers do not support distributed transcation(2PC).As in your case
the session beans and entity beans reside on different servers,the case might be that
container might not support Distributed Transcation,Hence changes might not be
updated.
Where can I learn (more) about Java's support for developing multi-
threaded programs?
Location: http://www.jguru.com/faq/view.jsp?EID=431248
Created: May 30, 2001
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)
What is the default time for transaction manager? And how to set maximum
time(timeout) for transaction?.
Location: http://www.jguru.com/faq/view.jsp?EID=1050395
Created: Jan 26, 2003
Author: Nick Maiorano (http://www.jguru.com/guru/viewbio.jsp?EID=780589)
Question originally posed by venkata devi prasad kankanalapalli
(http://www.jguru.com/guru/viewbio.jsp?EID=1034636
The default time depends on your app server. It is usually around 30 seconds. If you
are using bean-managed transactions, you can set it like this:
If you are using container-managed transactions, this value is set in a app server
specific way. Check your app server's deployment descriptor DTD.