ThreadLocal 을 사용 하여 발견

4196 단어 HibernateGo
더 읽 기
hibenate 클래스 ThreadLocal Session Context 에는 두 개의 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() )
세 션 이 비어 있 지 않 고 프 록 시 를 추가 한 적 이 없다 는 것 이다.
( Proxy.getInvocationHandler( session ) != null
       && ! ( Proxy.getInvocationHandler( session ) instanceof TransactionProtectionWrapper ) );프 록 시 를 추 가 했 지만 TransactionProtection Wrapper 가 아니라면 TransactionProtection Wrapper 는 무엇 일 까?
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;
				}
			}
		}

세 션 호출 방법 에 대해 제한 을 두 었 다 는 얘 기다.

좋은 웹페이지 즐겨찾기