使用ThreadLocal一些发现

在hibernate 类ThreadLocalSessionContext中有两段source code
	public final Session currentSession(); throws HibernateException {
		Session current = existingSession( factory );;
		if (current == null); {
			current = buildOrObtainSession();;
			// register a cleanup synch
			current.getTransaction();.registerSynchronization( buildCleanupSynch(); );;
			// wrap the session in the transaction-protection proxy
			if ( needsWrapping( current ); ); {
				current = wrap( current );;
			}
			// then bind it
			doBind( current, factory );;
		}
		return current;
	}

	private boolean needsWrapping(Session session); {
		// try to make sure we don't wrap and already wrapped session
		return session != null
		       && ! Proxy.isProxyClass( session.getClass(); );
		       || ( Proxy.getInvocationHandler( session ); != null
		       && ! ( Proxy.getInvocationHandler( session ); instanceof TransactionProtectionWrapper ); );;
	}



session != null
       && ! Proxy.isProxyClass( session.getClass() )
就是说session不为空,并且没有增加过proxy.
( Proxy.getInvocationHandler( session ) != null
       && ! ( Proxy.getInvocationHandler( session ) instanceof TransactionProtectionWrapper ) );就是说已经增加过proxy,但不是TransactionProtectionWrapper ,那么TransactionProtectionWrapper 是什么东东呢?
请看它的invoke方法:
		public Object invoke(Object proxy, Method method, Object[] args); throws Throwable {
			try {
				// If close(); is called, guarantee unbind();
				if ( "close".equals( method.getName();); ); {
					unbind( realSession.getSessionFactory(); );;
				}
				else if ( "toString".equals( method.getName(); );
					     || "equals".equals( method.getName(); );
					     || "hashCode".equals( method.getName(); );
				         || "getStatistics".equals( method.getName(); );
					     || "isOpen".equals( method.getName(); ); ); {
					// allow these to go through the the real session no matter what
				}
				else if ( !realSession.isOpen(); ); {
					// essentially, if the real session is closed allow any
					// method call to pass through since the real session
					// will complain by throwing an appropriate exception;
					// NOTE that allowing close(); above has the same basic effect,
					//   but we capture that there simply to perform the unbind...
				}
				else if ( !realSession.getTransaction();.isActive(); ); {
					// limit the methods available if no transaction is active
					if ( "beginTransaction".equals( method.getName(); );
					     || "getTransaction".equals( method.getName(); );
					     || "isTransactionInProgress".equals( method.getName(); );
					     || "setFlushMode".equals( method.getName(); );
					     || "getSessionFactory".equals( method.getName(); ); ); {
						log.trace( "allowing method [" + method.getName(); + "] in non-transacted context" );;
					}
					else if ( "reconnect".equals( method.getName(); );
					          || "disconnect".equals( method.getName(); ); ); {
						// allow these (deprecated); methods to pass through
					}
					else {
						throw new HibernateException( method.getName(); + " is not valid without active transaction" );;
					}
				}
				log.trace( "allowing proxied method [" + method.getName(); + "] to proceed to real session" );;
				return method.invoke( realSession, args );;
			}
			catch ( InvocationTargetException e ); {
				if ( e.getTargetException(); instanceof RuntimeException ); {
					throw ( RuntimeException ); e.getTargetException();;
				}
				else {
					throw e;
				}
			}
		}

就是说它对session调用的方法做了一些限制。

你可能感兴趣的:(Hibernate,Go)