本文整理汇总了Java中org.springframework.transaction.support.TransactionTemplate.setReadOnly方法的典型用法代码示例。如果您正苦于以下问题:Java TransactionTemplate.setReadOnly方法的具体用法?Java TransactionTemplate.setReadOnly怎么用?Java TransactionTemplate.setReadOnly使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.transaction.support.TransactionTemplate
的用法示例。
在下文中一共展示了TransactionTemplate.setReadOnly方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: afterPropertiesSet
import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
@Override
public void afterPropertiesSet() throws Exception {
// Our transaction template will be read-only
readonlyTransactionTemplate = new TransactionTemplate(transactionManager);
readonlyTransactionTemplate.setReadOnly(true);
cachedWriters = new HashMap<Class<?>, IndexWriter>();
statusFile = new File(indexHandler.getIndexRoot(), "status");
status = new Properties();
try {
status.load(new FileReader(statusFile));
long time = Long.parseLong(status.getProperty(LAST_OPERATION_TIME));
lastOperationTime = new GregorianCalendar();
lastOperationTime.setTimeInMillis(time);
lastOperationId = Long.parseLong(status.getProperty(LAST_OPERATION_ID));
} catch (Exception e) {
// Ok, ignore. We'll start with empty properties
lastOperationTime = null;
lastOperationId = null;
}
// Start the thread
thread = new Thread(this, "IndexOperationRunner");
thread.start();
}
示例2: testTransactionWithIsolationAndReadOnly
import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
@Test
public void testTransactionWithIsolationAndReadOnly() throws Exception {
given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
given(con.getAutoCommit()).willReturn(true);
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
tt.setReadOnly(true);
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
// something transactional
}
});
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
InOrder ordered = inOrder(con);
ordered.verify(con).setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
ordered.verify(con).setAutoCommit(false);
ordered.verify(con).commit();
ordered.verify(con).setAutoCommit(true);
ordered.verify(con).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
verify(con).close();
}
示例3: setTransactionManager
import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
public final void setTransactionManager(PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
transactionTemplate = new TransactionTemplate(transactionManager);
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
transactionTemplateReadOnly = new TransactionTemplate(transactionManager);
transactionTemplateReadOnly.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
transactionTemplateReadOnly.setReadOnly(true);
}
示例4: testTransactionCommitWithReadOnly
import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
@Test
public void testTransactionCommitWithReadOnly() throws Exception {
Connection con = mock(Connection.class);
final SessionFactory sf = mock(SessionFactory.class);
Session session = mock(Session.class);
Transaction tx = mock(Transaction.class);
Query query = mock(Query.class);
final List list = new ArrayList();
list.add("test");
given(sf.openSession()).willReturn(session);
given(session.beginTransaction()).willReturn(tx);
given(session.connection()).willReturn(con);
given(session.isOpen()).willReturn(true);
given(session.createQuery("some query string")).willReturn(query);
given(query.list()).willReturn(list);
given(session.isConnected()).willReturn(true);
given(con.isReadOnly()).willReturn(true);
HibernateTransactionManager tm = new HibernateTransactionManager(sf);
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setReadOnly(true);
assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
Object result = tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
HibernateTemplate ht = new HibernateTemplate(sf);
return ht.find("some query string");
}
});
assertTrue("Correct result list", result == list);
assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
verify(session).setFlushMode(FlushMode.MANUAL);
verify(con).setReadOnly(true);
verify(tx).commit();
verify(con).setReadOnly(false);
verify(session).close();
}
示例5: setTransactionManager
import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
@Inject
public void setTransactionManager(final PlatformTransactionManager transactionManager) {
transactionTemplate = new TransactionTemplate(transactionManager);
transactionTemplate.setReadOnly(true);
}