cmttest.java

来自「好东西,hibernate-3.2.0,他是一开元的树杖hibernate-3.」· Java 代码 · 共 510 行 · 第 1/2 页

JAVA
510
字号
//$Id: CMTTest.java 10421 2006-09-01 20:52:35Z steve.ebersole@jboss.com $
package org.hibernate.test.tm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Iterator;

import javax.transaction.Transaction;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.hibernate.EntityMode;
import org.hibernate.Session;
import org.hibernate.ScrollableResults;
import org.hibernate.ConnectionReleaseMode;
import org.hibernate.dialect.SybaseDialect;
import org.hibernate.transaction.CMTTransactionFactory;
import org.hibernate.util.SerializationHelper;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.criterion.Order;
import org.hibernate.test.TestCase;

/**
 * @author Gavin King
 */
public class CMTTest extends TestCase {
	
	public CMTTest(String str) {
		super(str);
	}
	
	public void testConcurrent() throws Exception {
		getSessions().getStatistics().clear();
		
		DummyTransactionManager.INSTANCE.begin();
		Session s = openSession();
		Map foo = new HashMap();
		foo.put("name", "Foo");
		foo.put("description", "a big foo");
		s.persist("Item", foo);
		Map bar = new HashMap();
		bar.put("name", "Bar");
		bar.put("description", "a small bar");
		s.persist("Item", bar);
		DummyTransactionManager.INSTANCE.commit();
		
		getSessions().evictEntity("Item");
		
		DummyTransactionManager.INSTANCE.begin();
		Session s1 = openSession();
		foo = (Map) s1.get("Item", "Foo");
		//foo.put("description", "a big red foo");
		//s1.flush();
		Transaction tx1 = DummyTransactionManager.INSTANCE.suspend();
		
		DummyTransactionManager.INSTANCE.begin();
		Session s2 = openSession();
		foo = (Map) s2.get("Item", "Foo");
		DummyTransactionManager.INSTANCE.commit();
		
		DummyTransactionManager.INSTANCE.resume(tx1);
		tx1.commit();
		
		getSessions().evictEntity("Item");

		DummyTransactionManager.INSTANCE.begin();
		s1 = openSession();
		s1.createCriteria("Item").list();
		//foo.put("description", "a big red foo");
		//s1.flush();
		tx1 = DummyTransactionManager.INSTANCE.suspend();
		
		DummyTransactionManager.INSTANCE.begin();
		s2 = openSession();
		s2.createCriteria("Item").list();
		DummyTransactionManager.INSTANCE.commit();
		
		DummyTransactionManager.INSTANCE.resume(tx1);
		tx1.commit();
		
		DummyTransactionManager.INSTANCE.begin();
		s2 = openSession();
		s2.createCriteria("Item").list();
		DummyTransactionManager.INSTANCE.commit();
		
		assertEquals( getSessions().getStatistics().getEntityLoadCount(), 7 );
		assertEquals( getSessions().getStatistics().getEntityFetchCount(), 0 );
		assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 3 );
		assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
		assertEquals( getSessions().getStatistics().getQueryCacheMissCount(), 0 );

		DummyTransactionManager.INSTANCE.begin();
		s = openSession();
		s.createQuery("delete from Item").executeUpdate();
		DummyTransactionManager.INSTANCE.commit();
	}
	
	public void testConcurrentCachedQueries() throws Exception {
		
		DummyTransactionManager.INSTANCE.begin();
		Session s = openSession();
		Map foo = new HashMap();
		foo.put("name", "Foo");
		foo.put("description", "a big foo");
		s.persist("Item", foo);
		Map bar = new HashMap();
		bar.put("name", "Bar");
		bar.put("description", "a small bar");
		s.persist("Item", bar);
		DummyTransactionManager.INSTANCE.commit();
		
		synchronized (this) { wait(1000); }

		getSessions().getStatistics().clear();
		
		getSessions().evictEntity("Item");

		DummyTransactionManager.INSTANCE.begin();
		Session s4 = openSession();
		Transaction tx4 = DummyTransactionManager.INSTANCE.suspend();

		DummyTransactionManager.INSTANCE.begin();
		Session s1 = openSession();
		List r1 = s1.createCriteria("Item").addOrder( Order.asc("description") )
				.setCacheable(true).list();
		assertEquals( r1.size(), 2 );
		Transaction tx1 = DummyTransactionManager.INSTANCE.suspend();

		DummyTransactionManager.INSTANCE.begin();
		Session s2 = openSession();
		List r2 = s2.createCriteria("Item").addOrder( Order.asc("description") )
				.setCacheable(true).list();
		assertEquals( r2.size(), 2 );
		DummyTransactionManager.INSTANCE.commit();
		
		assertEquals( getSessions().getStatistics().getSecondLevelCacheHitCount(), 2 );
		assertEquals( getSessions().getStatistics().getSecondLevelCacheMissCount(), 0 );
		assertEquals( getSessions().getStatistics().getEntityLoadCount(), 2 );
		assertEquals( getSessions().getStatistics().getEntityFetchCount(), 0 );
		assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
		assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 1 );
		assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 1 );
		assertEquals( getSessions().getStatistics().getQueryCacheMissCount(), 1 );
		
		DummyTransactionManager.INSTANCE.resume(tx1);
		tx1.commit();
		
		DummyTransactionManager.INSTANCE.begin();
		Session s3 = openSession();
		s3.createCriteria("Item").addOrder( Order.asc("description") )
				.setCacheable(true).list();
		DummyTransactionManager.INSTANCE.commit();
		
		assertEquals( getSessions().getStatistics().getSecondLevelCacheHitCount(), 4 );
		assertEquals( getSessions().getStatistics().getSecondLevelCacheMissCount(), 0 );
		assertEquals( getSessions().getStatistics().getEntityLoadCount(), 2 );
		assertEquals( getSessions().getStatistics().getEntityFetchCount(), 0 );
		assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
		assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 1 );
		assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 2 );
		assertEquals( getSessions().getStatistics().getQueryCacheMissCount(), 1 );
		
		DummyTransactionManager.INSTANCE.resume(tx4);
		List r4 = s4.createCriteria("Item").addOrder( Order.asc("description") )
			.setCacheable(true).list();
		assertEquals( r4.size(), 2 );
		tx4.commit();
		
		assertEquals( getSessions().getStatistics().getSecondLevelCacheHitCount(), 6 );
		assertEquals( getSessions().getStatistics().getSecondLevelCacheMissCount(), 0 );
		assertEquals( getSessions().getStatistics().getEntityLoadCount(), 2 );
		assertEquals( getSessions().getStatistics().getEntityFetchCount(), 0 );
		assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
		assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 1 );
		assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 3 );
		assertEquals( getSessions().getStatistics().getQueryCacheMissCount(), 1 );
		
		DummyTransactionManager.INSTANCE.begin();
		s = openSession();
		s.createQuery("delete from Item").executeUpdate();
		DummyTransactionManager.INSTANCE.commit();
	}
	
	public void testConcurrentCachedDirtyQueries() throws Exception {
		if ( getDialect() instanceof SybaseDialect ) {
			reportSkip( "dead-lock bug", "concurrent queries" );
			return;
		}

		DummyTransactionManager.INSTANCE.begin();
		Session s = openSession();
		Map foo = new HashMap();
		foo.put("name", "Foo");
		foo.put("description", "a big foo");
		s.persist("Item", foo);
		Map bar = new HashMap();
		bar.put("name", "Bar");
		bar.put("description", "a small bar");
		s.persist("Item", bar);
		DummyTransactionManager.INSTANCE.commit();
		
		synchronized (this) { wait(1000); }

		getSessions().getStatistics().clear();
		
		getSessions().evictEntity("Item");

		DummyTransactionManager.INSTANCE.begin();
		Session s4 = openSession();
		Transaction tx4 = DummyTransactionManager.INSTANCE.suspend();

		DummyTransactionManager.INSTANCE.begin();
		Session s1 = openSession();
		List r1 = s1.createCriteria("Item").addOrder( Order.asc("description") )
				.setCacheable(true).list();
		assertEquals( r1.size(), 2 );
		foo = (Map) r1.get(0);
		foo.put("description", "a big red foo");
		s1.flush();
		Transaction tx1 = DummyTransactionManager.INSTANCE.suspend();

		DummyTransactionManager.INSTANCE.begin();
		Session s2 = openSession();
		List r2 = s2.createCriteria("Item").addOrder( Order.asc("description") )
				.setCacheable(true).list();
		assertEquals( r2.size(), 2 );
		DummyTransactionManager.INSTANCE.commit();
		
		assertEquals( getSessions().getStatistics().getSecondLevelCacheHitCount(), 0 );
		assertEquals( getSessions().getStatistics().getSecondLevelCacheMissCount(), 0 );
		assertEquals( getSessions().getStatistics().getEntityLoadCount(), 4 );
		assertEquals( getSessions().getStatistics().getEntityFetchCount(), 0 );
		assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 2 );
		assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 2 );
		assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
		assertEquals( getSessions().getStatistics().getQueryCacheMissCount(), 2 );
		
		DummyTransactionManager.INSTANCE.resume(tx1);
		tx1.commit();
		
		DummyTransactionManager.INSTANCE.begin();
		Session s3 = openSession();
		s3.createCriteria("Item").addOrder( Order.asc("description") )
				.setCacheable(true).list();
		DummyTransactionManager.INSTANCE.commit();
		
		assertEquals( getSessions().getStatistics().getSecondLevelCacheHitCount(), 0 );
		assertEquals( getSessions().getStatistics().getSecondLevelCacheMissCount(), 0 );
		assertEquals( getSessions().getStatistics().getEntityLoadCount(), 6 );
		assertEquals( getSessions().getStatistics().getEntityFetchCount(), 0 );
		assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 3 );
		assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 3 );

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?