java - How to start a transaction in another thread? -
spring 4
.
i'm working on implementing updatable cache through scheduledthreadpoolexecutor
. looks follows:
public class myservice { @setter private mydao mydao; //immutable private volatile cache cache; //static factory method public static myservice create(){ myservice retval = new myservice(); cache = cache.emptycache(); updatecache(); } @transactional(propagation = propagation.requires_new) public int get(){ //retrieving cache return cache.getnext(); } private void updatecache() { try{ scheduledexecutorservice ses = executors.newscheduledthreadpool(1); //the runnable should in transaction. runnable updatecache; //the runnable queries mydao, //which in turn implemented db-dao //the cache assigned after computation ses.schedulewithfixeddelay(updatecache, 0, 60, timeunit.seconds); } catch (throwable t){ } } }
my question how can run runnable job in transaction?
annotating updatecache()
@transactional
won't work long transactions thread bound.
don't schedule that, use scheduling support of spring.
public class myservice { @setter private mydao mydao; //immutable private volatile cache cache; //static factory method public static myservice create(){ myservice retval = new myservice(); cache = cache.emptycache(); updatecache(); } @transactional(propagation = propagation.requires_new) public int get(){ //retrieving cache return cache.getnext(); } @transactional @scheduled(fixeddelay=60000) public void updatecache() { //the runnable queries mydao, //which in turn implemented db-dao //the cache assigned after computation } }
then either add @enablescheduling
or <task:annotation-driven />
. see earlier mentioned link more configuration options.
Comments
Post a Comment