SysGlobalCache & thread

Is it possible to access SysGlobalCache by thread ?

I don’t see any reason why it shouldn’t work - it’s just a class as any other. Did you get into any troubles (if you tried that)?

Nevertheless it definitely isn’t thread-safe.

I tried to set a key with a value in a thread.

When I tried to retreive it in a job, I had no data.

Maybe I’m doing something wrong…look below.

Thanks

public static str checkConfigThread(Thread t)

{

SysGlobalCache globalCache= ClassFactory.globalCache();

;

globalCache.set(curuserid(),‘66’,‘aaaa’);

}

static void Job109(Args _args)

{

SysGlobalCache globalCache= ClassFactory.globalCache();

info( globalCache.get(curuserid(),‘66’) ); // I was especting getting → aaaa

}

I believe that each thread has its own session, therefore one thread can’t read session-specific variables of another thread. SysGlobalCache can be used in threads, but not for sharing data across threads. Nevertheless I haven’t done any detailed research.

Look at SysGlobalObjectCache - it’s shared across sessions.

By the way, your code wouldn’t work even without threads, because the values is inserted on AOS and read on client - these are two separate instances of SysGlobalCache class.

By the way, your code wouldn’t work even without threads, because the values is inserted on AOS and read on client - these are two separate instances of SysGlobalCache class.

From what I red here, It should work

http://learnax.blogspot.ca/2010/01/globalcache-alternative-to.html

ClassFactory is a special class, which has instances residing at both server and client. At run time, two instances of the ClassFactory class exist, and they share name classFactory. However confusing the implementation details might sound, this is a powerful concept. When you call a method on classFactory from code running on the client, you are calling the classFactory object on the client tier; when you call a method on classFactory from code running on the server, you are calling the classFactory object on the server tier. Remember this when debugging the ClassFactory class.


But I’m looking at the SysGlobalObjectCache to see if it can help me… I’ll let you know , Thanks

By the way, your code wouldn’t work even without threads, because the values is inserted on AOS and read on client - these are two separate instances of SysGlobalCache class.

From what I red here, It should work

http://learnax.blogspot.ca/2010/01/globalcache-alternative-to.html

ClassFactory is a special class, which has instances residing at both server and client. At run time, two instances of the ClassFactory class exist, and they share name classFactory. However confusing the implementation details might sound, this is a powerful concept. When you call a method on classFactory from code running on the client, you are calling the classFactory object on the client tier; when you call a method on classFactory from code running on the server, you are calling the classFactory object on the server tier. Remember this when debugging the ClassFactory class.


But I’m looking at the SysGlobalObjectCache to see if it can help me… I’ll let you know , Thanks

Your quote confirms my point - there is one classFactory instance on client and another classFactory on server. Different instance don’t share instance variables, of course. Just test your code and you’ll see.

SysGlobalObjectCache is not available on V3.

I’ll try it when I have the chance to go back on 2012.

But in meantime I’m thinking of building a table for symulating the sysGlobalCache, with those columns:

thread_id, key, value, date_time_started, date_time_finish.

What you think of that?

Yes, I know that SysGlobalObjectCache doesn’t exist in Axapta 3, but I didn’t know you’re asking about that version. I mentioned it mainly to alert you

Database is definitely a reliable and easy-to-use shared resource. If you can use that (e.g. there is not problem with performance, rolled back transactions or so), use it.

I can’t comment your table since I don’t know how it will be used.

I had the same problem. My threads crashed when using SysGlobalCache object. What I found is that there is a difference when the command that launches the thread is executed on the client or on the server :

myThread.run(

classnum( classname ), staticmethodstr(classname, methodname ));

When this instruction is executed from the server the resulting thread-process crashes when using SysGlobalCache.

When this instruction is executed from the client the resulting thread works just fine.

I guess it’s a small bug in the kernel.

Mike, I can’t reproduce your problem. Can you show your code and mention your version of AX?

Hi, to be clear, I have no problem anymore, because I learned to launch threadsfrom client.

Anyway, in attached project you find 3 files , LedgerAllocationBasisRule : a standard table with 2 extra fields which are calculated by the program. FIDCalcBasisRuleAmountsMThr : a class that calculates these values, this class can run on server. FIDCalcBasisRuleAmountsThreaded, a class that creates, checkes, monitors the threads. IF you put this class on server the threads crash on SYSGlobalObjectCache. If the class runs on client the threads run fine. To find the exact cause I had to inject myself tracing code into the X++ code because the threads cannot be debugged.Actually the thread crashed on the code in class\LedgerCache::getValue.

PS: You will not be able to import and compile the project because of dependencies with other localisations. I will not spend time in setting up a small demo example that runs on AX standard. If you are very interested you can do that yourself.
Project_BasisRulesThreaded.zip (7.65 KB)

Sorry forgot : AXVersion = AX2012 R2 CU1