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

Ask Your Question
0

Transaction with Map API

I guess today is my play day with GigaSpaces. So let me run into another small problem. I tried to use pessimistic locking for transaction synchronization. However, the behavior wasn't as I expected. I thought if another process is working on an entry, the other thread should wait until it's timed out. However, it seems to throw CacheTimeoutException right away. This is the test code. If you notice from the code, after the another transaction is in process, it seems to fail fast instead of waiting until time out. Did I do something wrong?

import com.j_spaces.core.client.CacheTimeoutException;
import com.j_spaces.core.client.LocalTransactionManager;
import com.j_spaces.map.CacheFinder;
import com.j_spaces.map.IMap;
import net.jini.core.transaction.Transaction;
import net.jini.core.transaction.TransactionFactory;
import net.jini.core.transaction.server.TransactionManager;
....
    public void testCache() throws Exception
    {
        final IMap map = (IMap) CacheFinder.find("/./testCache?schema=mycache&versioned=false&create=true&noCache=false");
        Thread thread1 = new Thread()
        {
            public void run()
            {
                try
                {
                    TransactionManager trManager = LocalTransactionManager.getInstance(map);
                    Transaction.Created created = TransactionFactory.create(trManager, 60000);
                    Transaction transaction = created.transaction;
                    System.out.println("Thread-1: Start" + System.currentTimeMillis());
                    try
                    {
                        map.put("Test", "Test", transaction, 60000);
                    } catch (CacheTimeoutException ex)
                    {
                        System.out.println("Thread-1: Timeout" + System.currentTimeMillis());
                    }
                    System.out.println("Thread-1: After lock" + System.currentTimeMillis());
                    Thread.sleep(500);
                    System.out.println("Thread-1: End" + System.currentTimeMillis());
                    transaction.commit();
                } catch (Exception ex)
                {
                }
            }
        };
        thread1.start();
        Thread thread = new Thread()
        {
            public void run()
            {
                try
                {
                    TransactionManager trManager = LocalTransactionManager.getInstance(map);
                    Transaction.Created created = TransactionFactory.create(trManager, 60000);
                    Transaction transaction = created.transaction;
                    System.out.println("Thread-2: Start" + System.currentTimeMillis());
                    try
                    {
                        map.put("Test", "Test", transaction, 60000);
                    } catch (CacheTimeoutException ex)
                    {
                        System.out.println("Thread-2: Timeout" + System.currentTimeMillis());
                    }
                    System.out.println("Thread-2: After lock" + System.currentTimeMillis());
                    Thread.sleep(500);
                    System.out.println("Thread-2: End" + System.currentTimeMillis());
                    transaction.commit();
                } catch (Exception ex)
                {
                }
            }
        };
        thread.start();
        thread1.join();
        thread.join();
    }

This thread was imported from the previous forum.
For your reference, the original is available here

asked 2007-08-23 14:42:00 -0500

testy gravatar image

updated 2013-08-08 09:52:00 -0500

jaissefsfex gravatar image
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

The IMap.setWaitForResponse controls the timeout of the transaction (in fact it is the IJSPace. update call timeout parameter).
I would suggest you to move into more non-intrusive , Spring based way building your application using the polling container. This seems to provide you exactly what you need without being bound to specific interface. You will get also the Spring Transaction support out of the box.
Just make it Spring!
See:
http://www.gigaspaces.com/wiki/display/GS6/OpenSpacesEventsComponent-PollingContainer

Shay

answered 2007-08-23 16:13:00 -0500

shay hassidim gravatar image
edit flag offensive delete link more
0

This one, I understand that you can set IMap.setWaitForResponse(XXXX); However, isn't that the point to use an argument "long waitForResponse". This argument should be exposed for put and remove method as well to remove the need to call IMap.setWaitForResponse(XXXX) explicitly since different calls that share the same instance of IMap might want to use difference waitForResponse time out.

answered 2007-08-23 14:57:02 -0500

testy gravatar image
edit flag offensive delete link more

Comments

Another point I would like to make is that waiting for someone to put the value into the cache and waiting for someone to finish the transaction are not the same thing. I might be wrong but waitForResponse seems to apply for both case. I would want IMap API to behave more like readIfExists rather than read where it will return right away if the entry doesn't exist. However, if the entry is participated in a transaction, it will wait until it times out.

testy gravatar image testy  ( 2007-08-23 16:05:01 -0500 )edit

The reason I tried to use setWaitForResponse is because IMap.get() function did not wait for the other transaction to finish, it just fails immediately. If I use OpenSpaces API, I still need to cope with this problem since I will get Cache/IMap interface back anyway. The scenation mentioned in the link is probably good for polling mechanism not for pushing mechanism.

testy gravatar image testy  ( 2007-08-23 16:22:02 -0500 )edit

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2007-08-23 14:42:00 -0500

Seen: 39 times

Last updated: Aug 23 '07