Welcome to the new Gigaspaces XAP forum. To recover your account, please follow these instructions.

Ask Your Question
0

How do I start a transaction?

 Basically I want to be able to use transactions without having to use xml.
My code is as follows:

PlatformTransactionManager ptm = new DistributedJiniTxManagerConfigurer().commitTimeout(2000L).defaultTimeout(30).transactionManager();
GigaSpace gigaSpace = new GigaSpaceConfigurer(configurer).transactionManager(ptm).gigaSpace(); DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
definition.setIsolationLevel(Isolation.READ_COMMITTED.value());
definition.setPropagationBehavior(Propagation.REQUIRED.ordinal()); 
/* If I changed to mandatory there is an exception as no transaction exists*/
definition.setName("transaction-"+transactionId);
TransactionStatus status = ptm.getTransaction(definition);
gigaspaces.write(object);
ptm.commit(status);
status = ptm.getTransaction(definition);
gigaspaces.write(object);
ptm.rollback(status);
object = gigaspaces.write(object);
object // This come out to be null

asked 2017-05-19 01:38:30 -0600

akacoder007 gravatar image
edit retag flag offensive close merge delete

4 Answers

Sort by ยป oldest newest most voted
1

/** * Sep 5, 2013 */ package com.gigaspaces.support.client;

import org.openspaces.core.GigaSpace; import org.openspaces.core.GigaSpaceConfigurer; import org.openspaces.core.space.UrlSpaceConfigurer; import org.openspaces.core.transaction.manager.DistributedJiniTxManagerConfigurer; import org.springframework.transaction.PlatformTransactionManager;

import com.gigaspaces.lrmi.LRMIManager; import com.gigaspaces.management.GigaSpacesRuntime;

/** * @author jb * */ public abstract class GigaClient {

private final static String DASH = "-";
private final static String DDASH = "--";
private final static String JINI = "jini://";
private final static String EMBED = "/./";
private final static String WILD = "*/*/";
private final static String LOCATE = "?locators=";
private final static String GROUP = "?groups=";
private final static String TIMEOUT = "&timeout=";

private static String LOCAL = "localhost";
private static String SPACE = "space";

private static String PARAMS = "\t-%c %-9s - %s";

private String locators;
private String groups;
private String name;

private Boolean transaction = false;

private String user;
private String pass;

private String url;

private Boolean embed = false;
private Boolean versioned = false;

private Integer timeout = 5000; // the default

private UrlSpaceConfigurer urlConfig;
private GigaSpaceConfigurer gigaConfig;

protected GigaSpace space;
protected PlatformTransactionManager ptm;

public static enum Parameters {

    SPACE('s', "space", "the name of the space"),
    VERSIONED('v', "versioned", "space objects versioned - used for optimistic locking"),
    LOCATORS('l', "locators", "the names of lookup services hosts"),
    GROUPS('g', "groups", "the locator groups"),
    EMBED('e', "embed", "whether or not to create an embedded space - default = false"),
    TIMEOUT('o', "timeout", "the length of time before the lookup should timeout, in millis"),
    TRANSACT('t', "transaction", "embeds a transaction manager in the space - default = false"),
    URL('r', "space url", "the space url to use - bypass internal url method"),
    USER('u', "user", "the user credential"),
    PASS('p', "pass", "the user's password"),
    HELP('h', "help", "show this message"),
    ORDER('i', "orderid", "the orderID to search for in the space");

    private Character flag;
    private String label;
    private String desc;

    private Parameters(Character flag, String label, String desc) {
        this.flag = flag;
        this.label = label;
        this.desc = desc;
    }

// public Character getFlag() { return flag; } public String getFlag() { return DASH + flag; } public String getLabel() { return label; } public String getLabelFlag() { return DDASH + label; } public String getDesc() { return desc; } }

public String getSpaceUrl() {

    if (embed) return EMBED + name;
    else {
        if (locators != null && groups != null ) 
            return JINI + WILD + name + LOCATE + locators + "&groups=" + groups + TIMEOUT + timeout;            
        if (locators != null) 
            return JINI + WILD + name + LOCATE + locators + TIMEOUT + timeout; 
        if (groups != null)
            return JINI + WILD + name + GROUP + groups + TIMEOUT + timeout; 
    }
    System.out.println("unresolvable lookup");
    System.exit(-99);
    return null;
}

public boolean getParams(String[] args) {

    Boolean help = false;

    try {
        for (int a = 0; a < args.length; a++) {
            if (args[a].equals(Parameters.SPACE.getFlag())) name = args[++a];
            if (args[a].equals(Parameters.VERSIONED.getFlag())) {
                System.out.println("using a versioned space");
                versioned = Boolean.valueOf(true);
            }
            if (args[a].equals(Parameters.LOCATORS.getFlag())) locators = args[++a];
            if (args[a].equals(Parameters.GROUPS.getFlag())) groups = args[++a];
            if (args[a].equals(Parameters.EMBED.getFlag())) {
                System.out.println("using an embedded space");
                embed = Boolean.valueOf(true);
            }
            if (args[a].equals(Parameters.TRANSACT.getFlag())) {
                System.out.println("using a transaction manager");
                transaction = Boolean.valueOf(true); 
            }
            if (args[a].equals(Parameters.USER.getFlag()))  user = args[++a];
            if (args[a].equals(Parameters.PASS.getFlag()))  pass = args[++a];
            if (args[a].equals(Parameters.URL.getFlag()))  url = args[++a];
            if (args[a].equals(Parameters.HELP.getFlag())) help = Boolean.valueOf(true);
            if (args[a].equals(Parameters.TIMEOUT.getFlag())) timeout = Integer.valueOf(args[++a]);
        }
    } catch (Exception e) {
        e.printStackTrace();
        help = true;
    }

    return help;
}

public void init(String[] args) {

    if (getParams(args)) showHelpMessage(); 

    // defaults if necessary
    if (name == null) name = SPACE; 
    if (locators == null && groups == null ) {
        locators = LOCAL;
    }

    long now = 0L;

    if (url == null) url = getSpaceUrl();
    System.out.println("url: " + url);

    urlConfig = (new UrlSpaceConfigurer(url)).versioned(versioned);
    if (user != null && pass != null) 
        urlConfig.credentials(user, pass);

    try {
        gigaConfig = new GigaSpaceConfigurer(urlConfig.space());
        if (transaction) {
            try {
                ptm = new DistributedJiniTxManagerConfigurer().transactionManager();
                space = gigaConfig.transactionManager(ptm).gigaSpace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        else {
            now = System.currentTimeMillis();
            space = gigaConfig.gigaSpace();
        }
    } catch (Exception fe) {
        System.out.println(fe.getMessage());
        System.out.println("\nlookup time: " + (System.currentTimeMillis() - now));
        System.exit(-1);
    }

    System.out.println("\nlookup time: " + (System.currentTimeMillis() - now));
}


/*************************************************************************
 * Actual work done here.
 * -s space -g group_name
 *************************************************************************/
public abstract void run();

public void run(String[] args) {

    init(args);
    run();
    shutdown();
}

public void shutdown() {

    System.out.println("calling shutdown");
    try {
        urlConfig.destroy();

// urlConfig.close(); GigaSpacesRuntime.shutdown(); LRMIManager.shutdown(); } catch (Exception e) { e.printStackTrace(); } }

public static void showHelpMessage() {

    System.out.println("GigaSpace Test Platform");
    System.out.println("Parameters:");
    for (Parameters params : Parameters.values()) {
        System.out.println(String.format(PARAMS, params.getFlag(), params.getLabel(), params.getDesc()));
    }
    System.exit(0);
}

public String getLocators() {
    return locators;
}

public String getGroups() {
    return groups;
}

public String getName() {
    return name;
}

public Boolean getTransaction() {
    return transaction;
}

public String getUser() {
    return user;
}

public String getPass() {
    return pass;
}

public Boolean getEmbed() {
    return embed;
}

public GigaSpace getSpace() {
    return space;
}

}

answered 2017-05-22 14:10:50 -0600

jb gravatar image
edit flag offensive delete link more
0

Not sure why it makes sense for you to use EmbeddedSpaceConfigurer - unless you have a unit test.

This is how you use a remote proxy for a space deployed as usual:

PlatformTransactionManager ptm = new DistributedJiniTxManagerConfigurer().commitTimeout(2000L).defaultTimeout(30).transactionManager();
GigaSpace gigaSpace = new GigaSpaceConfigurer(new SpaceProxyConfigurer("space")).transactionManager(ptm).gigaSpace();
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
definition.setIsolationLevel(Isolation.READ_COMMITTED.value());
definition.setPropagationBehavior(Propagation.REQUIRES_NEW.ordinal()); 
TransactionStatus status = ptm.getTransaction(definition);
gigaSpace.write(d);
ptm.commit(status);

answered 2017-05-22 13:59:00 -0600

shay hassidim gravatar image
edit flag offensive delete link more
0

The return object from the write operation is null by default.

Place a breakpoint before the commit and check the GS UI transaction list. You should see your transaction listed.

Have few min timeout so it won't expire.

answered 2017-05-20 11:43:42 -0600

shay hassidim gravatar image
edit flag offensive delete link more

Comments

Just before gigaspaces.write(object); gigaspace.getCurrentTransaction() comes out to be null. Though the issue was solved by constructing gigapsaces object as follows:

  EmbeddedSpaceConfigurer configurer = ....
        PlatformTransactionManager ptm = new DistributedJiniTxManagerConfigurer().transactionManager();
        GigaSpace gigaSpace = new GigaSpaceConfigurer(configurer).defaultIsolationLevel(2).transactionManager(ptm).create();
akacoder007 gravatar imageakacoder007 ( 2017-05-21 18:49:36 -0600 )edit
0

create a class with a main entry point

instantiate the class

MyClient extends GIgaClient

main

MyClient mc = new MyClient();

mc.run(args);

you class should create a no-arg method called 'run'; it is here that you should do your work

In you overridden 'run' method you will have a gigaspace, embedded or remote depending on the parameters you pass, secure or not, and with an injected transaction manager if configured appropriately. Then simply follow the code that Shay has provided. Get the TM from the space, create a transaction definition with propagation = REQUIRES_NEW, then execute some code and call commit on the transaction status.

HtH

John

answered 2017-05-22 14:15:31 -0600

jb gravatar image
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2017-05-19 01:38:30 -0600

Seen: 444 times

Last updated: May 22