Thursday Dec 10, 2009

RollBack Transaction Command

The rollback-transaction command rolls back the specific transaction (identified by transaction-id).

This command may be used when any of the following situations (which i can think of) occur
1) the transaction is hanging on the server waiting for a resource
2) for some reasons transaction is taking long time to execute.

Following are the steps which demonstrates how to use the rollback-transaction command


1) To roll back the transaction you need to enable the monitoring for transaction service. This is done by executing the following command
asadmin set server.monitoring-service.module-monitoring-levels.transaction-service=HIGH

2) Deployed an application which does an active transaction for a minute like adding values to a table in a database.

3) When you want to rollback a transaction, freeze the transaction service to lock it by executing the following command
asadmin freeze-transaction-service

4) Get the list of active transaction by executing the following command
asadmin get -m "server.transaction-service.\*"
server.transaction-service.activecount-count = 1
server.transaction-service.activecount-description = Provides the number of transactions that are currently active.
server.transaction-service.activecount-lastsampletime = 1254431281781
server.transaction-service.activecount-name = ActiveCount
server.transaction-service.activecount-starttime = 1254430982059
server.transaction-service.activecount-unit = count
server.transaction-service.activeids-current =
Transaction Id                          Status                   ElapsedTime(ms)          ComponentName                                   ResourceNames

0000000000000001_00                     Active                   68566                    org.nachi.transactiontest.ejb.SimpleSessionBean jdbc/__default,
server.transaction-service.activeids-description = Provides the IDs of the transactions that are currently active a.k.a. in-flight transactions. Every such transaction can be rolled back after freezing the transaction service.
server.transaction-service.activeids-lastsampletime = 1254431350346
server.transaction-service.activeids-name = ActiveIds
server.transaction-service.activeids-starttime = 1254430982061
server.transaction-service.activeids-unit = List
server.transaction-service.committedcount-count = 0
server.transaction-service.committedcount-description = Provides the number of transactions that have been committed.
server.transaction-service.committedcount-lastsampletime = -1
server.transaction-service.committedcount-name = CommittedCount
server.transaction-service.committedcount-starttime = 1254430982059
server.transaction-service.committedcount-unit = count
server.transaction-service.dotted-name = server.transaction-service
server.transaction-service.rolledbackcount-count = 0
server.transaction-service.rolledbackcount-description = Provides the number of transactions that have been rolled back.
server.transaction-service.rolledbackcount-lastsampletime = -1
server.transaction-service.rolledbackcount-name = RolledbackCount
server.transaction-service.rolledbackcount-starttime = 1254430982059
server.transaction-service.rolledbackcount-unit = count
server.transaction-service.state-current = True
server.transaction-service.state-description = Indicates if the transaction service has been frozen
server.transaction-service.state-lastsampletime = 1254431350346
server.transaction-service.state-name = State
server.transaction-service.state-starttime = 1254430982063
server.transaction-service.state-unit = String

5) From the output you can observe that there is one active transaction. Say you want to rollback the active transaction. Execute the following command with the specific transaction id
asadmin rollback-transaction 0000000000000001_00


6)  Check the status of the transactions by executing the following command
asadmin get -m "server.transaction-service.\*"
From the output fragment you can observe that transaction is marked as rollback
Transaction Id                          Status                   ElapsedTime(ms)          ComponentName                                   ResourceNames

0000000000000001_00                     Rollback                   68566                    org.nachi.transactiontest.ejb.SimpleSessionBean jdbc/__default

7) Unfreeze transaction service and check if the active transaction is cancelled (No values should have been added to the table).

In the next blog i will be talking about recover transaction cli command.

How to add a new cli command (asadmin utlilty) for performing administrative task in glassfish v3 server

Glassfish v3 application server is designed to be modular and extensible so that you can add/remove functionality as you require. In this blog i am going to talk about how easy it is to add a new admin cli command (asadmin utility) which can be used to perform administrative task for glassfish application server from command line and also give the dependencies required for the command implementation.

This blog has been written with reference to other blogs Sreeni Blog I am giving two examples in the blog. The first example is about adding some more functionality to the existing command list-applications. Currently list-applications lists all the applications with the type information. Now in my new command implementation, i adding some code to display the context root along with application status.

First Example


The new command is added in the module deployment-admin.

Declaration
The new command is implemented as a Service using annotation @Service named list-applications-detail and by implementing the AdminCommand interface. Scope of the command is defined as @Scoped, PerLookup, so that a new instance is created when the command is executed.

@Service(name = "list-applications-detail")
@Scoped(PerLookup.class)
Execute Method

ListComponentsCommand (available in deployment-admin module) is extended and the execute method of
AdminCommand interface is overriden for adding the new functionality.

public class ListApplicationsDetail extends ListComponentsCommand { 
@Inject
ApplicationRegistry appRegistry;
final private static LocalStringManagerImpl localStrings = new LocalStringManagerImpl(ListApplicationsDetail.class)
public void execute(AdminCommandContext context) {
final ActionReport report = context.getActionReport();
String status = "Disabled";
ActionReport.MessagePart part = report.getTopMessagePart();
int numOfApplications = 0;
for (Named module : applications.getModules()) {
if (module instanceof Application) {
final Application app = (Application) module;
if (app.getEnabled().equalsIgnoreCase("true"))
status = "Enabled";
if (app.getObjectType().equals("user")) {
if (type == null || isApplicationOfThisType(app, type)) {
ActionReport.MessagePart childPart = part.addChild();
childPart.setMessage(app.getName() + " " +
getAppSnifferEngines(app, true) + " " + getContextRoot(app)
+ " " + status);
numOfApplications++;
}
}
}
}
if (numOfApplications == 0) {
part.setMessage(localStrings.getLocalString("list.components.no.elements.to.list", "Nothing to List."));
}
report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}

   private String getWebModuleName(List appModules) {
for (Module appMod : appModules) {
List engines = appMod.getEngines();
for (Engine engine : engines) {
if (engine.getSniffer().equals("web"))
return appMod.getName();
}
}
return null;
}

   private String getContextRoot(Application app) {
String moduleName = getWebModuleName(app.getModule());
ApplicationInfo appInfo = appRegistry.get(app.getName());
if (appInfo != null) {
com.sun.enterprise.deployment.Application appDeployObj = appInfo.getMetaData(com.sun.enterprise.deployment.Application.class);
if (appDeployObj != null) {
BundleDescriptor bundleDesc = appDeployObj.getModuleByUri(moduleName);
if (bundleDesc != null &&
bundleDesc instanceof WebBundleDescriptor) {
return ((WebBundleDescriptor) bundleDesc).getContextRoot();
}
}
}
return null;
}
}

Dependencies required

 <dependency>
<groupid>com.sun.enterprise</groupid>
<artifactid>hk2</artifactid>
</dependency>
<dependency>
<groupid>org.glassfish.common</groupid>
<artifactid>glassfish-api</artifactid>
<version>${project.version}</version>
</dependency>
<dependency>
<groupid>org.glassfish.deployment</groupid>
<artifactid>dol</artifactid>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupid>org.glassfish.deployment</groupid>
<artifactid>deployment-admin</artifactid>
<version>${project.version}</version>
</dependency>
The dependency required are hk2 for implementing the cli command as a service so that it can can be automatically and dynamically discovered by the runtime. The other dependency required is glassfish-api which has Public APIs of Glassfish V3 and deployment-dol which contains Deployment Object Library and deployment-admin which contains Deployment Related Admin Classes

Command Execution and Output

After you compile the deployment-admin module copy the deployment-admin.jar file to <glassfish-install>/modules directory and start your application server (asadmin start-domain).

asadmin list-commands|grep list-applications-detail
create-http-listener list-applications-detail
asadmin list-applications-detail
converter <ear, ejb,="" web,="" appclient=""> /converter Enabled
logging-helloworld <ear, ejb,="" web=""> logging-helloworld Enabled
jdbc-simple <ear, ejb,="" web=""> jdbc-simple Enabled
RosterApp <ear, ejb,="" web=""> /RosterClient Enabled
Command list-applications-detail executed successfully.
From the output you can observe that context root and application status has been displayed along with application deployed and type.

Second Example

This example is very generic to return hello world when you execute the command.

Declaration

The new command is implemented as a Service using annotation @Service named sampleclicommand and by implementing the AdminCommand interface. Scope of the command is defined as @Scoped, PerLookup, so that a new instance is created when the command is executed.

@Service(name="sampleclicommand")
@Scoped(PerLookup.class)
Execute Method

Implement the execute method of AdminCommand interface for providing the functionality of the command.ActionReport class obtained from AdminContext of glassfish-api is used to return the output and exit status to the client.

 public class SampleCliCommand implements AdminCommand {
public void execute(AdminCommandContext context) {
ActionReport report = context.getActionReport();
report.setMessage("Hello World!!!");
report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
}
Dependencies required
<dependency>
<groupid>com.sun.enterprise</groupid>
<artifactid>hk2</artifactid>
</dependency>
<dependency>
<groupid>org.glassfish.common</groupid>
<artifactid>glassfish-api</artifactid>
<version>${project.version}</version>
</dependency>

The dependency required are hk2 for implementing the cli command as a service so that it can can be automatically and dynamically discovered by the runtime.The other dependency required is glassfish-api which has Public APIs of Glassfish V3.

Command Execution and Output

After you compile the module copy to <glassfish-install>/modules directory and start your application server (asadmin start-domain).

Execute following steps (with appropriate changes) to see if your command works 
asadmin list-commands |grep sampleclicommand
delete-network-listener sampleclicommand
asadmin sampleclicommand
Hello World!!!
Command sampleclicommand executed successfully.

About

Nachiappan Veerappan Nachiappan

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today