📄 sessionfactoryimpl.java
字号:
} static interface FilterCacheKeyFactory { //Will not recalculate hashKey for constant queries Object newInstance(String role, String query, boolean scalar); } //TODO: this stuff can be implemented in separate class to reuse soft MRU/LRU caching private synchronized Object get(Object key) { Object result = softQueryCache.get(key); if( result != null ) { strongRefs[ ++strongRefIndex % MAX_STRONG_REF_COUNT ] = result; } return result; } private void put(Object key, Object value) { softQueryCache.put(key, value); strongRefs[ ++strongRefIndex % MAX_STRONG_REF_COUNT ] = value; } private synchronized QueryTranslator[] createQueryTranslators( String[] concreteQueryStrings, Object cacheKey, Map enabledFilters ) { final int length = concreteQueryStrings.length; final QueryTranslator[] queries = new QueryTranslator[length]; for ( int i=0; i<length; i++ ) { queries[i] = settings.getQueryTranslatorFactory() .createQueryTranslator( concreteQueryStrings[i], enabledFilters, this ); } if (cacheKey != null) put(cacheKey, queries); return queries; } private synchronized FilterTranslator createFilterTranslator( String filterString, Object cacheKey, Map enabledFilters ) { final FilterTranslator filter = settings.getQueryTranslatorFactory() .createFilterTranslator(filterString, enabledFilters, this); if (cacheKey != null) put(cacheKey, filter); return filter; } private Map checkNamedQueries() throws HibernateException { // Check named queries Map errors = new HashMap(); Set names = namedQueries.keySet(); log.info("Checking " + namedQueries.size() + " named queries"); for ( Iterator iterator = names.iterator(); iterator.hasNext(); ) { String queryName = (String) iterator.next(); NamedQueryDefinition q = (NamedQueryDefinition) namedQueries.get(queryName); // this will throw an error if there's something wrong. try { log.debug("Checking named query: " + queryName); //TODO: BUG! this currently fails for named queries for non-POJO entities getQuery( q.getQueryString(), false, CollectionHelper.EMPTY_MAP ); } catch (QueryException e) { errors.put(queryName, e); } catch (MappingException e) { errors.put(queryName, e); } } return errors; } public QueryTranslator[] getQuery(String queryString, boolean shallow, Map enabledFilters) throws QueryException, MappingException { // if there are no enabled filters, consider cached query compilations, // otherwise generate/compile a new set of query translators Object cacheKey = null; QueryTranslator[] queries = null; if ( enabledFilters == null || enabledFilters.isEmpty() ) { cacheKey = QUERY_KEY_FACTORY.newInstance(queryString, shallow); queries = (QueryTranslator[]) get(cacheKey); } // have to be careful to ensure that if the JVM does out-of-order execution // then another thread can't get an uncompiled QueryTranslator from the cache // we also have to be very careful to ensure that other threads can perform // compiled queries while another query is being compiled if ( queries==null ) { // a query that names an interface or unmapped class in the from clause // is actually executed as multiple queries String[] concreteQueryStrings = QuerySplitter.concreteQueries(queryString, this); queries = createQueryTranslators(concreteQueryStrings, cacheKey, enabledFilters); } for ( int i=0; i<queries.length; i++) {// queries[i].compile(this, settings.getQuerySubstitutions(), shallow, enabledFilters); queries[i].compile( settings.getQuerySubstitutions(), shallow ); } // see comment above. note that QueryTranslator.compile() is synchronized return queries; } public FilterTranslator getFilter( String filterString, String collectionRole, boolean scalar, Map enabledFilters) throws QueryException, MappingException { Object cacheKey = null; FilterTranslator filter = null; if ( enabledFilters == null || enabledFilters.isEmpty() ) { cacheKey = FILTER_KEY_FACTORY.newInstance(collectionRole, filterString, scalar); filter = (FilterTranslator) get(cacheKey); } if ( filter==null ) { filter = createFilterTranslator(filterString, cacheKey, enabledFilters); }// filter.compile(collectionRole, this, settings.getQuerySubstitutions(), scalar, enabledFilters); filter.compile( collectionRole, settings.getQuerySubstitutions(), scalar ); // see comment above. note that FilterTranslator.compile() is synchronized return filter; } private SessionImpl openSession( Connection connection, boolean autoClose, long timestamp, Interceptor sessionLocalInterceptor ) { return new SessionImpl( connection, this, autoClose, timestamp, sessionLocalInterceptor, sessionEventListenerConfig, settings.getDefaultEntityMode(), settings.isFlushBeforeCompletionEnabled(), settings.isAutoCloseSessionEnabled() ); } public org.hibernate.classic.Session openSession(Connection connection, Interceptor sessionLocalInterceptor) { return openSession(connection, false, Long.MIN_VALUE, sessionLocalInterceptor); } public org.hibernate.classic.Session openSession(Interceptor sessionLocalInterceptor) throws HibernateException { // note that this timestamp is not correct if the connection provider // returns an older JDBC connection that was associated with a // transaction that was already begun before openSession() was called // (don't know any possible solution to this!) long timestamp = settings.getCacheProvider().nextTimestamp(); return openSession( null, true, timestamp, sessionLocalInterceptor ); } public org.hibernate.classic.Session openSession(Connection connection) { return openSession(connection, interceptor); //prevents this session from adding things to cache } public org.hibernate.classic.Session openSession() throws HibernateException { return openSession(interceptor); } public org.hibernate.classic.Session getCurrentSession() throws HibernateException { if ( transactionManager == null ) { throw new HibernateException( "No TransactionManagerLookup specified" ); } Transaction txn = null; try { txn = transactionManager.getTransaction(); // We could register the session against the transaction even though it is // not started, but we'd have no guarentee of ever getting the map // entries cleaned up. if ( !JTAHelper.isInProgress( txn.getStatus() ) ) { throw new HibernateException( "Current transaction is not in progress" ); } } catch( SystemException se ) { throw new HibernateException( "Unable to locate current transaction" ); } if ( currentSessionMap == null ) { currentSessionMap = new Hashtable(); } org.hibernate.classic.Session currentSession = ( org.hibernate.classic.Session ) currentSessionMap.get( txn ); if ( currentSession == null ) { currentSession = new SessionImpl( null, this, true, settings.getCacheProvider().nextTimestamp(), interceptor, sessionEventListenerConfig, settings.getDefaultEntityMode(), settings.isFlushBeforeCompletionEnabled(), settings.isAutoCloseSessionEnabled() ); try { txn.registerSynchronization( new CurrentSessionCleanupSynch( txn, this ) ); } catch( Throwable t ) { throw new HibernateException( "Unable to register cleanup Synchronization with TransactionManager" ); } currentSessionMap.put( txn, currentSession ); } return currentSession; } public EntityPersister getEntityPersister(String entityName) throws MappingException { EntityPersister result = (EntityPersister) entityPersisters.get(entityName); if (result==null) { throw new MappingException( "Unknown entity: " + entityName ); } return result; } public CollectionPersister getCollectionPersister(String role) throws MappingException { CollectionPersister result = (CollectionPersister) collectionPersisters.get(role); if (result==null) { throw new MappingException( "Unknown collection role: " + role ); } return result; } public Settings getSettings() { return settings; } public Dialect getDialect() { return settings.getDialect(); } public TransactionFactory getTransactionFactory() { return settings.getTransactionFactory(); } public TransactionManager getTransactionManager() { return transactionManager; } public SQLExceptionConverter getSQLExceptionConverter() { return settings.getSQLExceptionConverter(); } // from javax.naming.Referenceable public Reference getReference() throws NamingException { log.debug("Returning a Reference to the SessionFactory"); return new Reference( SessionFactoryImpl.class.getName(), new StringRefAddr("uuid", uuid), SessionFactoryObjectFactory.class.getName(), null ); } private Object readResolve() throws ObjectStreamException { log.trace("Resolving serialized SessionFactory"); // look for the instance by uuid Object result = SessionFactoryObjectFactory.getInstance(uuid); if (result==null) { // in case we were deserialized in a different JVM, look for an instance with the same name // (alternatively we could do an actual JNDI lookup here....) result = SessionFactoryObjectFactory.getNamedInstance(name); if (result==null) { throw new InvalidObjectException("Could not find a SessionFactory named: " + name); } else { log.debug("resolved SessionFactory by name"); } } else { log.debug("resolved SessionFactory by uid"); } return result; } public NamedQueryDefinition getNamedQuery(String queryName) { return (NamedQueryDefinition) namedQueries.get(queryName); } public NamedSQLQueryDefinition getNamedSQLQuery(String queryName) { return (NamedSQLQueryDefinition) namedSqlQueries.get(queryName); } public Type getIdentifierType(String className) throws MappingException { return getEntityPersister(className).getIdentifierType(); } public String getIdentifierPropertyName(String className) throws MappingException { return getEntityPersister(className).getIdentifierPropertyName(); } private final void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { log.trace("deserializing"); in.defaultReadObject(); log.debug("deserialized: " + uuid); } private final void writeObject(ObjectOutputStream out) throws IOException { log.debug("serializing: " + uuid); out.defaultWriteObject(); log.trace("serialized"); } public Type[] getReturnTypes(String queryString) throws HibernateException { return getRepresentativeQuery(queryString).getReturnTypes();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -