cmttest.java
来自「hibernate-3.0.5 中文文档」· Java 代码 · 共 374 行
JAVA
374 行
//$Id: CMTTest.java,v 1.15 2005/05/25 03:34:28 oneovthafew Exp $package org.hibernate.test.tm;import java.util.HashMap;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.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 s1 = openSession(); s1.createCriteria("Item").addOrder( Order.asc("description") ) .setCacheable(true).list(); //foo.put("description", "a big red foo"); //s1.flush(); Transaction tx1 = DummyTransactionManager.INSTANCE.suspend(); DummyTransactionManager.INSTANCE.begin(); Session s2 = openSession(); s2.createCriteria("Item").addOrder( Order.asc("description") ) .setCacheable(true).list(); DummyTransactionManager.INSTANCE.commit(); DummyTransactionManager.INSTANCE.resume(tx1); tx1.commit(); DummyTransactionManager.INSTANCE.begin(); s2 = openSession(); s2.createCriteria("Item").addOrder( Order.asc("description") ) .setCacheable(true).list(); DummyTransactionManager.INSTANCE.commit(); assertEquals( getSessions().getStatistics().getEntityLoadCount(), 0 ); assertEquals( getSessions().getStatistics().getEntityFetchCount(), 0 ); assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 0 ); 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 testCMT() throws Exception { getSessions().getStatistics().clear(); DummyTransactionManager.INSTANCE.begin(); Session s = openSession(); DummyTransactionManager.INSTANCE.getTransaction().commit(); assertFalse( s.isOpen() ); assertEquals( getSessions().getStatistics().getFlushCount(), 0 ); DummyTransactionManager.INSTANCE.begin(); s = openSession(); DummyTransactionManager.INSTANCE.getTransaction().rollback(); assertFalse( s.isOpen() ); DummyTransactionManager.INSTANCE.begin(); s = openSession(); Map item = new HashMap(); item.put("name", "The Item"); item.put("description", "The only item we have"); s.persist("Item", item); DummyTransactionManager.INSTANCE.getTransaction().commit(); assertFalse( s.isOpen() ); DummyTransactionManager.INSTANCE.begin(); s = openSession(); item = (Map) s.createQuery("from Item").uniqueResult(); assertNotNull(item); s.delete(item); DummyTransactionManager.INSTANCE.getTransaction().commit(); assertFalse( s.isOpen() ); assertEquals( getSessions().getStatistics().getTransactionCount(), 4 ); assertEquals( getSessions().getStatistics().getSuccessfulTransactionCount(), 3 ); assertEquals( getSessions().getStatistics().getEntityDeleteCount(), 1 ); assertEquals( getSessions().getStatistics().getEntityInsertCount(), 1 ); assertEquals( getSessions().getStatistics().getSessionOpenCount(), 4 ); assertEquals( getSessions().getStatistics().getSessionCloseCount(), 4 ); assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 ); assertEquals( getSessions().getStatistics().getFlushCount(), 2 ); DummyTransactionManager.INSTANCE.begin(); s = openSession(); s.createQuery("delete from Item").executeUpdate(); DummyTransactionManager.INSTANCE.commit(); } public void testCurrentSession() throws Exception { DummyTransactionManager.INSTANCE.begin(); Session s = getSessions().getCurrentSession(); Session s2 = getSessions().getCurrentSession(); assertSame( s, s2 ); DummyTransactionManager.INSTANCE.getTransaction().commit(); assertFalse( s.isOpen() ); // TODO : would be nice to automate-test that the SF internal map actually gets cleaned up // i verified that is does currently in my debugger... } public void testCurrentSessionWithIterate() throws Exception { DummyTransactionManager.INSTANCE.begin(); Session s = openSession(); Map item1 = new HashMap(); item1.put( "name", "Item - 1" ); item1.put( "description", "The first item" ); s.persist( "Item", item1 ); Map item2 = new HashMap(); item2.put( "name", "Item - 2" ); item2.put( "description", "The second item" ); s.persist( "Item", item2 ); DummyTransactionManager.INSTANCE.getTransaction().commit(); // First, test iterating the partial iterator; iterate to past // the first, but not the second, item DummyTransactionManager.INSTANCE.begin(); s = getSessions().getCurrentSession(); Iterator itr = s.createQuery( "from Item" ).iterate(); if ( !itr.hasNext() ) { fail( "No results in iterator" ); } itr.next(); if ( !itr.hasNext() ) { fail( "Only one result in iterator" ); } DummyTransactionManager.INSTANCE.getTransaction().commit(); // Next, iterate the entire result DummyTransactionManager.INSTANCE.begin(); s = getSessions().getCurrentSession(); itr = s.createQuery( "from Item" ).iterate(); if ( !itr.hasNext() ) { fail( "No results in iterator" ); } while ( itr.hasNext() ) { itr.next(); } DummyTransactionManager.INSTANCE.getTransaction().commit(); DummyTransactionManager.INSTANCE.begin(); s = openSession(); s.createQuery( "delete from Item" ).executeUpdate(); DummyTransactionManager.INSTANCE.getTransaction().commit(); } public void testCurrentSessionWithScroll() throws Exception { DummyTransactionManager.INSTANCE.begin(); Session s = openSession(); Map item1 = new HashMap(); item1.put( "name", "Item - 1" ); item1.put( "description", "The first item" ); s.persist( "Item", item1 ); Map item2 = new HashMap(); item2.put( "name", "Item - 2" ); item2.put( "description", "The second item" ); s.persist( "Item", item2 ); DummyTransactionManager.INSTANCE.getTransaction().commit(); // First, test partially scrolling the result (w/o closing) DummyTransactionManager.INSTANCE.begin(); s = getSessions().getCurrentSession(); ScrollableResults results = s.createQuery( "from Item" ).scroll(); results.next(); DummyTransactionManager.INSTANCE.getTransaction().commit(); // Next, scroll the entire result (w/o closing) DummyTransactionManager.INSTANCE.begin(); s = getSessions().getCurrentSession(); results = s.createQuery( "from Item" ).scroll(); while( !results.isLast() ) { results.next(); } DummyTransactionManager.INSTANCE.getTransaction().commit(); DummyTransactionManager.INSTANCE.begin(); s = getSessions().getCurrentSession(); results = s.createQuery( "from Item" ).scroll(); while( !results.isLast() ) { results.next(); } results.close(); DummyTransactionManager.INSTANCE.getTransaction().commit(); DummyTransactionManager.INSTANCE.begin(); s = openSession(); s.createQuery( "delete from Item" ).executeUpdate(); DummyTransactionManager.INSTANCE.getTransaction().commit(); } public void testAggressiveReleaseWithExplicitDisconnectReconnect() throws Exception { DummyTransactionManager.INSTANCE.begin(); Session s = getSessions().getCurrentSession(); s.createQuery( "from Item" ).list(); s.disconnect(); byte[] bytes = SerializationHelper.serialize( s ); s = ( Session ) SerializationHelper.deserialize( bytes ); s.reconnect(); s.createQuery( "from Item" ).list(); DummyTransactionManager.INSTANCE.getTransaction().commit(); } public void testAggressiveReleaseWithConnectionRetreival() throws Exception { DummyTransactionManager.INSTANCE.begin(); Session s = openSession(); Map item1 = new HashMap(); item1.put( "name", "Item - 1" ); item1.put( "description", "The first item" ); s.save( "Item", item1 ); Map item2 = new HashMap(); item2.put( "name", "Item - 2" ); item2.put( "description", "The second item" ); s.save( "Item", item2 ); DummyTransactionManager.INSTANCE.getTransaction().commit(); try { DummyTransactionManager.INSTANCE.begin(); s = getSessions().getCurrentSession(); s.createQuery( "from Item" ).scroll().next(); s.connection(); DummyTransactionManager.INSTANCE.getTransaction().commit(); } finally { DummyTransactionManager.INSTANCE.begin(); s = openSession(); s.createQuery( "delete from Item" ).executeUpdate(); DummyTransactionManager.INSTANCE.getTransaction().commit(); } } protected String[] getMappings() { return new String[] { "tm/Item.hbm.xml" }; } public String getCacheConcurrencyStrategy() { return "transactional"; } public static Test suite() { return new TestSuite(CMTTest.class); } protected void configure(Configuration cfg) { cfg.setProperty(Environment.CONNECTION_PROVIDER, DummyConnectionProvider.class.getName()); cfg.setProperty(Environment.TRANSACTION_MANAGER_STRATEGY, DummyTransactionManagerLookup.class.getName()); cfg.setProperty(Environment.AUTO_CLOSE_SESSION, "true"); cfg.setProperty(Environment.FLUSH_BEFORE_COMPLETION, "true"); cfg.setProperty(Environment.RELEASE_CONNECTIONS, ConnectionReleaseMode.AFTER_STATEMENT.toString()); cfg.setProperty(Environment.GENERATE_STATISTICS, "true"); cfg.setProperty(Environment.USE_QUERY_CACHE, "true"); cfg.setProperty(Environment.DEFAULT_ENTITY_MODE, EntityMode.MAP.toString()); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?