mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Heinous merge from HEAD. Seems to basically work. Be on guard however.
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/WCM-DEV2/root@4137 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -45,6 +45,10 @@ public class DummyTransactionService implements TransactionService
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setReadOnly(boolean readOnly)
|
||||
{
|
||||
}
|
||||
|
||||
public UserTransaction getUserTransaction()
|
||||
{
|
||||
return txn;
|
||||
|
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Copyright (C) 2006 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.transaction;
|
||||
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
|
||||
|
||||
/**
|
||||
* A transactionally-safe storage class for singleton objects. Changes to the singleton
|
||||
* are only visibly promoted when the transaction is committed.
|
||||
* <p>
|
||||
* <code>
|
||||
* private static final TransactionAwareSingleton<Integer> MY_SINGLETON = new TransactionAwareSingleton<Integer>();
|
||||
* </code>
|
||||
* <p>
|
||||
* All modifications to the singleton via {@link #get()} and {@link #put(T)} are made in a
|
||||
* transaction-local manner and promoted to the shared value in a thread-safe manner upon
|
||||
* transacton completion. Transaction-local changes take precedence over the shared value.
|
||||
*
|
||||
* @see org.alfresco.repo.transaction.AlfrescoTransactionSupport
|
||||
*
|
||||
* @author Derek Hulley
|
||||
*/
|
||||
public class TransactionAwareSingleton<T> extends TransactionListenerAdapter
|
||||
{
|
||||
private static final String TRANSACTION_KEY = "TransactionAwareSingleton.storage";
|
||||
|
||||
private final ReadLock singletonReadLock;
|
||||
private final WriteLock singletonWriteLock;
|
||||
private Object singletonValue;
|
||||
|
||||
public TransactionAwareSingleton()
|
||||
{
|
||||
ReentrantReadWriteLock serverReadWriteLock = new ReentrantReadWriteLock();
|
||||
singletonReadLock = serverReadWriteLock.readLock();
|
||||
singletonWriteLock = serverReadWriteLock.writeLock();
|
||||
}
|
||||
|
||||
private void setValue(Object value)
|
||||
{
|
||||
// get a write lock
|
||||
singletonWriteLock.lock();
|
||||
try
|
||||
{
|
||||
singletonValue = value;
|
||||
}
|
||||
finally
|
||||
{
|
||||
singletonWriteLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private Object getValue()
|
||||
{
|
||||
// get a read lock
|
||||
singletonReadLock.lock();
|
||||
try
|
||||
{
|
||||
return singletonValue;
|
||||
}
|
||||
finally
|
||||
{
|
||||
singletonReadLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns the transaction- and thread-safe wrapped instance
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public T get()
|
||||
{
|
||||
// an in-transaction value overrides the singleton
|
||||
TransactionStorage storage = (TransactionStorage) AlfrescoTransactionSupport.getResource(TRANSACTION_KEY);
|
||||
if (storage != null)
|
||||
{
|
||||
return (T) storage.newValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (T) getValue();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the value in a transaction- and thread-safe manner. It will only be persisted
|
||||
* at the end of the transaction but will be visible to the current transaction from
|
||||
* this call onwards.
|
||||
*
|
||||
* @param value the value to store
|
||||
*/
|
||||
public void put(T value)
|
||||
{
|
||||
// the value is changing
|
||||
TransactionStorage storage = (TransactionStorage) AlfrescoTransactionSupport.getResource(TRANSACTION_KEY);
|
||||
if (storage == null)
|
||||
{
|
||||
// it has not changed before
|
||||
storage = new TransactionStorage();
|
||||
AlfrescoTransactionSupport.bindResource(TRANSACTION_KEY, storage);
|
||||
// listen to the transaction
|
||||
AlfrescoTransactionSupport.bindListener(this);
|
||||
}
|
||||
storage.newValue = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Promotes the storage value to the single value, if required
|
||||
*/
|
||||
public void afterCommit()
|
||||
{
|
||||
TransactionStorage storage = (TransactionStorage) AlfrescoTransactionSupport.getResource(TRANSACTION_KEY);
|
||||
if (storage != null)
|
||||
{
|
||||
// the value was overridden
|
||||
setValue(storage.newValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* In-transaction storage of the altered value
|
||||
* @author Derek Hulley
|
||||
*/
|
||||
private static class TransactionStorage
|
||||
{
|
||||
public Object newValue;
|
||||
}
|
||||
}
|
@@ -0,0 +1,224 @@
|
||||
/*
|
||||
* Copyright (C) 2006 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.transaction;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import javax.transaction.UserTransaction;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.alfresco.repo.transaction.TransactionUtil.TransactionWork;
|
||||
import org.alfresco.service.transaction.TransactionService;
|
||||
import org.alfresco.util.ApplicationContextHelper;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.transaction.TransactionAwareSingleton
|
||||
*
|
||||
* @author Derek Hulley
|
||||
*/
|
||||
public class TransactionAwareSingletonTest extends TestCase
|
||||
{
|
||||
private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
|
||||
private static Random rand = new Random();
|
||||
|
||||
/** the instance to test */
|
||||
private TransactionAwareSingleton<Integer> singleton = new TransactionAwareSingleton<Integer>();
|
||||
private static final Integer INTEGER_ONE = new Integer(1);
|
||||
private static final Integer INTEGER_TWO = new Integer(2);
|
||||
|
||||
private TransactionService transactionService;
|
||||
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
transactionService = (TransactionService) ctx.getBean("transactionComponent");
|
||||
}
|
||||
|
||||
public void testCommit() throws Throwable
|
||||
{
|
||||
UserTransaction txn = transactionService.getUserTransaction();
|
||||
try
|
||||
{
|
||||
txn.begin();
|
||||
|
||||
singleton.put(INTEGER_ONE);
|
||||
check(INTEGER_ONE, true);
|
||||
check(null, false);
|
||||
|
||||
// commit
|
||||
txn.commit();
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
try { txn.rollback(); } catch (Throwable ee) {}
|
||||
throw e;
|
||||
}
|
||||
check(INTEGER_ONE, true);
|
||||
check(INTEGER_ONE, false);
|
||||
}
|
||||
|
||||
public void testRollback() throws Throwable
|
||||
{
|
||||
UserTransaction txn = transactionService.getUserTransaction();
|
||||
try
|
||||
{
|
||||
txn.begin();
|
||||
|
||||
singleton.put(INTEGER_TWO);
|
||||
check(INTEGER_TWO, true);
|
||||
check(null, false);
|
||||
|
||||
// rollback
|
||||
txn.rollback();
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
try { txn.rollback(); } catch (Throwable ee) {}
|
||||
throw e;
|
||||
}
|
||||
check(null, true);
|
||||
check(null, false);
|
||||
}
|
||||
|
||||
private static final int THREAD_COUNT = 20;
|
||||
public void testThreadsCommit() throws Throwable
|
||||
{
|
||||
TestThread[] threads = new TestThread[THREAD_COUNT];
|
||||
for (int i = 0; i < THREAD_COUNT; i++)
|
||||
{
|
||||
TestThread thread = new TestThread(true);
|
||||
thread.start();
|
||||
threads[i] = thread;
|
||||
}
|
||||
// wait for them to complete
|
||||
for (int i = 0; i < THREAD_COUNT; i++)
|
||||
{
|
||||
while (threads[i].finished == false)
|
||||
{
|
||||
synchronized(this)
|
||||
{
|
||||
try { wait(20); } catch (Throwable e) {}
|
||||
}
|
||||
}
|
||||
if (threads[i].error != null)
|
||||
{
|
||||
throw threads[i].error;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void testThreadsRollback() throws Throwable
|
||||
{
|
||||
TestThread[] threads = new TestThread[THREAD_COUNT];
|
||||
for (int i = 0; i < THREAD_COUNT; i++)
|
||||
{
|
||||
TestThread thread = new TestThread(false);
|
||||
thread.start();
|
||||
threads[i] = thread;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dumps random values into
|
||||
* @author Derek Hulley
|
||||
*/
|
||||
private class TestThread extends Thread
|
||||
{
|
||||
private boolean finished = false;
|
||||
private Throwable error;
|
||||
private boolean commit;
|
||||
private Integer value = new Integer((int)System.nanoTime());
|
||||
|
||||
public TestThread(boolean commit)
|
||||
{
|
||||
this.commit = commit;
|
||||
}
|
||||
@Override
|
||||
public synchronized void run()
|
||||
{
|
||||
UserTransaction txn = transactionService.getUserTransaction();
|
||||
try
|
||||
{
|
||||
txn.begin();
|
||||
|
||||
singleton.put(value);
|
||||
|
||||
// wait for some random time
|
||||
try
|
||||
{
|
||||
wait((long)(rand.nextDouble() * 1000.0)); // wait up to a second
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
// ignore
|
||||
}
|
||||
|
||||
// check
|
||||
check(value, true);
|
||||
|
||||
if (commit)
|
||||
{
|
||||
txn.commit();
|
||||
}
|
||||
else
|
||||
{
|
||||
// rollback
|
||||
txn.rollback();
|
||||
}
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
try { txn.rollback(); } catch (Throwable ee) {}
|
||||
this.error = e;
|
||||
}
|
||||
if (!commit)
|
||||
{
|
||||
try
|
||||
{
|
||||
// no thread changes
|
||||
check(null, false);
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
error = e;
|
||||
}
|
||||
}
|
||||
finished = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void check(final Integer expected, boolean inTransaction)
|
||||
{
|
||||
TransactionWork<Object> checkWork = new TransactionWork<Object>()
|
||||
{
|
||||
public Object doWork() throws Exception
|
||||
{
|
||||
Integer actual = singleton.get();
|
||||
assertTrue("Values don't match: " + expected + " != " + actual, actual == expected);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
if (inTransaction)
|
||||
{
|
||||
TransactionUtil.executeInUserTransaction(transactionService, checkWork);
|
||||
}
|
||||
else
|
||||
{
|
||||
TransactionUtil.executeInNonPropagatingUserTransaction(transactionService, checkWork);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user