A portion of the disclosure of this patent document contains material which is subject to copyright protection. The copyright owner has no objection to the facsimile reproduction by anyone of the patent document or the patent disclosure, as it appears in the Patent and Trademark Office patent file or records, but otherwise reserves all copyright rights whatsoever.
This invention relates to the field of transactions using EJB instances.
A container runs as part of an application server to provide enterprise applications with a high level of abstraction using server-side application components that encapsulate the business logic of the applications. Here server-side application components can be instances of a programmable class (also referred to as class), which can be but is not limited to, a Java® bean, an Enterprise Java® Bean (EJB), an interface, a module, and other suitable concepts, and EJB will be used to illustrate the various embodiments of the invention in the following context. Two important types of EJBs are: the entity bean, which is a component that represents an object-oriented view of some entities stored in a persistent storage, such as a database, and the session bean, which is a component in the container that represents some business logic running on the application server. A session bean can be either stateful or stateless.
An EJB container may initiate and manage one or more transactions, each of which is started by a method in an EJB instance in the container. Each transaction of the method allows a client to perform business logic and/or exchange information. A container-managed transaction allows the container to demarcate a transaction using declarative instructions specified in a deployment descriptor, which allows the editing of both the structural and application assembly information of the container. One benefit of such declarative container-managed transactions is that, under such model, the application designer and/or user does not need to explicitly begin or end any transactions in an application code. The transaction boundaries are determined dynamically before and after the invocation of an EJB method by the container.
Transactions that are started on behalf of an EJB method are expected to fail occasionally. For example, operating in highly concurrent environments may yield database deadlocks, or optimistic concurrency exceptions may occur if optimistic concurrency is in effect. Both of these conditions will result in transactions being rolled back due to commitment failure to an underlying persistent data store, such as the database. In these cases, it is standard programming practice to write application code to expect the failure and to retry the failed action. A typical programming pattern for accomplishing this can be, but is not limited to enclosing the EJB method within a try catch block, which checks to see if an exception such as TransactionRolledBackException has been thrown. Such programming practice however, may add extra burden on the developer and make the source code tedious. In addition, it may also cause performance degradation when the method is invoked remotely over a network, which can be an internet or intranet.
The invention is illustrated by way of example and not by way of limitation in the figures of the accompanying drawings in which like references indicate similar elements. It should be noted that references to “an” or “one” embodiment in this disclosure are not necessarily to the same embodiment, and such references mean at least one.
Systems and methods of the present invention provide the ability of automatic retry of container-managed transactions by configuring the EJB container so that it automatically retries container-managed transactions that have been rolled back. More specifically, on a per method basis, the container can be instructed to retry a failed transaction as using a new one up to a specified number of times if that method was the originator of the transaction that has been rolled back. The enabling of automatic transaction retries is accomplished either at deployment time by setting properties in the deployment descriptor of the EJB, or at runtime by setting properties on the EJB via a console on the application server, such as the WebLogic Server.
The automatic retry of container-managed transactions is a convenient feature that provides non-programmatic ways for enabling the retrying of failed transactions that are initiated by an EJB method. There are a number of significant advantages gained by this feature, including:
The automatic retry of container-managed transactions is for use with session and entity beans that use container-managed transaction demarcation. Message driven beans are excluded because they already have this functionality. When using container-managed transaction demarcation, instances of message driven beans do not acknowledge the receipt of a message that they are processing when the transaction bracketing the receipt of the message is rolled back; thus the message will be retried in a new transaction. Timer Beans are also excluded because they already have this functionality.
Referring to
Referring to
In some embodiments, a method invoked may have a container-managed transaction attribute such as “RequiresNew” or “Required” defined. The automatic retry of transactions will only occur for those methods on whose behalf the EJB container starts a transaction. For those methods that have a transaction attribute of “RequiresNew”, the EJB container will always begin a new transaction when those methods are called and retry of transactions will occur if those methods are configured to retry transactions. On the other hand, for those methods that have a transaction attribute of “Required”, the EJB container will only start a new transaction if the methods were called without a transaction already in effect. If a transaction is not already in effect when such a method is called, then a new transaction is started and retry of transactions will occur if the method is configured to retry transactions. If a transaction is already in effect when such a method is called, then the method will inherit the transaction of its caller, a new transaction will not be started, and retry of transactions will not occur even if retry of transactions is configured.
In some embodiments, a method should be safe for re-invocation after failure. For example, a retry of the failed transaction should yield results that are the same as they would have been had the previous attempt succeeded. More specifically:
In some embodiments, the deployment descriptor may set properties of the container using an XML schema instance file, such as, the ejb-jar.xml and weblogic-ejb-jar.xsd. No changes are required to the standard ejb-jar.xml schema. A new element “retry-method-on-rollback” will be added to the weblogic-ejb-jar.xsd schema as shown by an exemplary syntax below:
This element describes a method or a set of methods that will be retried if a specified method starts a new transaction and if that transaction fails to successfully commit. An exemplary syntax of the “retry-method-on-rollback” element is shown as follows:
The “retry-count” element specifies the maximum number of times to retry a method whose transaction has failed to commit. The automatic retry of container-managed transactions is enabled and disabled by specifying this per method property. Values of “n” greater than “0” will result in the retrying of a method up to “n” times and values of less or equal to “0” will result in no retrying of the method, effectively disabling the feature. Only methods having a container-managed transaction attribute of “Required” or “RequiresNew” are allowed to have a retry-count>0. If a method is assigned a non-zero retry-count value but it does not have a container-managed transaction attribute of either “Required” or “RequiresNew”, then this should be considered an error by the EJB compiler.
In some embodiments, the feature of automatic retry of container-managed transactions can be enabled from the console at runtime after an application has been started. This allows system administrators to enable method retries on running applications, e.g., when it is noticed that an operation is encountering optimistic rollbacks fairly regularly. The administrator looks up the operation in the application reference manual and notes that the application designer has specified that the operation may be safely retried by a particular method. The administrator then uses the console to turn on the retries for that method.
In some embodiments, the retry count of a transaction can be modified at runtime via the console. There is an option “Set Retry Method On Rollback” for each bean deployed in the container. After selecting this option, a list of business methods that have transaction attribute “Required” or “RequiresNew” will appear. The user will be able to select a method from this list and view or modify the value of the retry-count attribute for that method. Modified values of the retry-count should be updated in the deployment descriptor of deployed bean, such as weblogic-ejb-jar.xml. For the consistency of the experience of the console users, the user interface supporting this feature may resemble the interface used for runtime modification of EJB per method security policies.
In some embodiments, a per bean method transaction rollback counter can be introduced into the console to keep track of the number of commit failures encountered for a given method. This may facilitate in diagnosing methods that use or that may be candidates for the feature of automatic retry of container-managed transactions.
In some embodiments, the feature of automatic retry of container-managed transactions may affect the performance of the business application involved. When the feature is not enabled or if it is enabled but the target method is not retried at runtime, there will be an almost negligible impact on performance due to the checking that is required to know that there is no need for a method retry. When the feature is used to retry a method, however, there may be a gain in performance when compared with having application code explicitly perform a method retry after catching a rollback exception. The improvement in performance is a result of the EJB container only having to setup and tear down the execution environment once for the initial method invoke and for all subsequent method re-invokes as opposed to application code effectively having to setup and tear down separately for each method invoke and re-invoke. If the method is invoked remotely, there will also be a performance gain to be had by not having to throw a remote exception from the server to the client and by not having the client re-invoke the method remotely.
The following exemplary scenarios illustrate the feature of automatic retry of container-managed transactions. Although they depict sets of codes for purposes of illustration, the EJB Finder is not limited to any particular code or arrangement of implementation. One skilled in the art will appreciate that the various composition of codes portrayed in the following could be omitted, rearranged, combined and/or adapted in various ways.
It is assumed the exemplary scenario contains an EJB container with instances of the following beans, all of which use container-managed transaction demarcation:
The ProcureBean 301 can be specified by the deployment descriptor in XML file ejb-jar.xml, as shown by the exemplary source code in
To increase overall system throughput, the system designers have decided that access to the parts data will use optimistic concurrency, which loads all relevant EJB instances, such as instances of PartBean, into the memory under the “optimistic” expectation that they are unlikely to be modified by another transaction in the duration of the current transaction. However, as parts prices as well as availability are subject to change at any time, it is possible that by the time the procure(..) method commits, the parts information in the instances of PartBean used to compute the order have become out-of-date, consequently, an optimistic concurrency exception is thrown and the order instances of OrderBean must be recomputed using updated parts data. The automatic transaction retry feature for procure(int, float) method 404 of the ProcureBean 403 can be enabled via elements in the “retry-method-on-rollback” element 401 in the weblogic-ejb-jar.xml deployment descriptor, as shown in
Referring to
One embodiment may be implemented using a conventional general purpose or a specialized digital computer or microprocessor(s) programmed according to the teachings of the present disclosure, as will be apparent to those skilled in the computer art. Appropriate software coding can readily be prepared by skilled programmers based on the teachings of the present disclosure, as will be apparent to those skilled in the software art. The invention may also be implemented by the preparation of integrated circuits or by interconnecting an appropriate network of conventional component circuits, as will be readily apparent to those skilled in the art.
One embodiment includes a computer program product which is a machine readable medium (media) having instructions stored thereon/in which can be used to program one or more computing devices to perform any of the features presented herein. The machine readable medium can include, but is not limited to, one or more types of disks, including floppy disks, optical discs, DVD, CD-ROMs, micro drive, and magneto-optical disks, ROMs, RAMs, EPROMs, EEPROMs, DRAMs, VRAMs, flash memory devices, magnetic or optical cards, nanosystems (including molecular memory ICs), or any type of media or device suitable for storing instructions and/or data.
Stored on any one of the computer readable medium (media), the present invention includes software for controlling both the hardware of the general purpose/specialized computer or microprocessor, and for enabling the computer or microprocessor to interact with a human user or other mechanism utilizing the results of the present invention. Such software may include, but is not limited to, device drivers, operating systems, execution environments/containers, and applications.
The foregoing description of the preferred embodiments of the present invention has been provided for the purposes of illustration and description. It is not intended to be exhaustive or to limit the invention to the precise forms disclosed. Many modifications and variations will be apparent to the practitioner skilled in the art. Particularly, while the concept “method” is used in the embodiments of the systems and methods described above, it will be evident that such concept can be interchangeably used with equivalent concepts such as, function, procedure, and other suitable concepts; while the concept “transaction” is used in the embodiments of the systems and methods described above, it will be evident that such concept can be interchangeably used with equivalent concepts such as, session, conversation and other suitable concepts. Embodiments were chosen and described in order to best describe the principles of the invention and its practical application, thereby enabling others skilled in the art to understand the invention, the various embodiments and with various modifications that are suited to the particular use contemplated. It is intended that the scope of the invention be defined by the following claims and their equivalents.
This application claims priority from the following applications, which are hereby incorporated by reference in their entireties: U.S. Provisional Patent Application No. 60/573,218, entitled SYSTEMS AND METHODS FOR AUTOMATIC RETRY OF TRANSACTIONS by Thorick Chow et al., filed May. 21, 2004 (Attorney Docket No. BEAS-01608US0 SRM/DTX).
Number | Date | Country | |
---|---|---|---|
60573218 | May 2004 | US |