cmttest.java

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

JAVA
510
字号
		assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
		assertEquals( getSessions().getStatistics().getQueryCacheMissCount(), 3 );
		
		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(), 2 );
		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 );
		assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 1 );
		assertEquals( getSessions().getStatistics().getQueryCacheMissCount(), 3 );
		
		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 = getSessions().getCurrentSession();
		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 with out closing
		DummyTransactionManager.INSTANCE.begin();
		s = getSessions().getCurrentSession();
		ScrollableResults results = s.createQuery( "from Item" ).scroll();
		results.next();
		DummyTransactionManager.INSTANCE.getTransaction().commit();

		// Next, test partially scrolling the result with closing
		DummyTransactionManager.INSTANCE.begin();
		s = getSessions().getCurrentSession();
		results = s.createQuery( "from Item" ).scroll();
		results.next();
		results.close();
		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();

		// Next, scroll the entire result (closing)
		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 = getSessions().getCurrentSession();
		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.TRANSACTION_STRATEGY, CMTTransactionFactory.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 + -
显示快捷键?