📄 localsessionfactory.java
字号:
* and registered with the TopLink DatabaseLogin instance (either the default
* instance or one passed in through the "databaseLogin" property). The
* "usesExternalConnectionPooling" flag will automatically be set to "true".
* @see oracle.toplink.sessions.DatabaseLogin#setConnector(oracle.toplink.sessions.Connector)
* @see oracle.toplink.sessions.DatabaseLogin#setUsesExternalConnectionPooling(boolean)
* @see #setDatabaseLogin(oracle.toplink.sessions.DatabaseLogin)
*/
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
/**
* Specify the TopLink DatabasePlatform instance that the Session should use:
* for example, HSQLPlatform. This is an alternative to specifying the platform
* in a <login> tag in the <code>sessions.xml</code> configuration file.
* <p>A passed-in DatabasePlatform will be registered with the TopLink
* DatabaseLogin instance (either the default instance or one passed in
* through the "databaseLogin" property).
* @see oracle.toplink.internal.databaseaccess.HSQLPlatform
* @see oracle.toplink.platform.database.HSQLPlatform
*/
public void setDatabasePlatform(DatabasePlatform databasePlatform) {
this.databasePlatform = databasePlatform;
}
/**
* Specify a TopLink SessionLog instance to use for detailed logging of the
* Session's activities: for example, DefaultSessionLog (which logs to the
* console), JavaLog (which logs through JDK 1.4'S <code>java.util.logging</code>,
* available as of TopLink 10.1.3), or CommonsLoggingSessionLog /
* CommonsLoggingSessionLog904 (which logs through Commons Logging,
* on TopLink 10.1.3 and 9.0.4, respectively).
* <p>Note that detailed Session logging is usually only useful for debug
* logging, with adjustable detail level. As of TopLink 10.1.3, TopLink also
* uses different log categories, which allows for fine-grained filtering of
* log messages. For standard execution, no SessionLog needs to be specified.
* @see oracle.toplink.sessions.DefaultSessionLog
* @see oracle.toplink.logging.DefaultSessionLog
* @see oracle.toplink.logging.JavaLog
* @see org.springframework.orm.toplink.support.CommonsLoggingSessionLog
* @see org.springframework.orm.toplink.support.CommonsLoggingSessionLog904
*/
public void setSessionLog(SessionLog sessionLog) {
this.sessionLog = sessionLog;
}
/**
* Create a TopLink SessionFactory according to the configuration settings.
* @return the new TopLink SessionFactory
* @throws TopLinkException in case of errors
*/
public SessionFactory createSessionFactory() throws TopLinkException {
if (logger.isInfoEnabled()) {
logger.info("Initializing TopLink SessionFactory from [" + this.configLocation + "]");
}
// Determine class loader to use.
ClassLoader classLoader =
(this.sessionClassLoader != null ? this.sessionClassLoader : ClassUtils.getDefaultClassLoader());
// Initialize the TopLink Session, using the configuration file
// and the session name.
DatabaseSession session = loadDatabaseSession(this.configLocation, this.sessionName, classLoader);
// It is possible for SessionManager to return a null Session!
if (session == null) {
throw new IllegalStateException(
"A session named " + this.sessionName + " could not be loaded from resource [" +
this.configLocation + "] using ClassLoader [" + classLoader + "]. " +
"This is most likely a deployment issue: Can the class loader access the resource?");
}
// Override default DatabaseLogin instance with specified one, if any.
if (this.databaseLogin != null) {
session.setLogin(this.databaseLogin);
}
// Override default connection pool with specified DataSource, if any.
if (this.dataSource != null) {
session.getLogin().setConnector(new JNDIConnector(this.dataSource));
session.getLogin().setUsesExternalConnectionPooling(true);
}
// Override default DatabasePlatform with specified one, if any.
if (this.databasePlatform != null) {
session.getLogin().usePlatform(this.databasePlatform);
}
// Override default SessionLog with specified one, if any.
if (this.sessionLog != null) {
session.setSessionLog(this.sessionLog);
session.logMessages();
}
// Log in and create corresponding SessionFactory.
session.login();
return newSessionFactory(session);
}
/**
* Load the specified DatabaseSession from the TopLink <code>sessions.xml</code>
* configuration file.
* @param configLocation the class path location of the <code>sessions.xml</code> file
* @param sessionName the name of the TopLink Session in the configuration file
* @param sessionClassLoader the class loader to use
* @return the DatabaseSession instance
* @throws TopLinkException in case of errors
*/
protected DatabaseSession loadDatabaseSession(
String configLocation, String sessionName, ClassLoader sessionClassLoader)
throws TopLinkException {
SessionManager manager = getSessionManager();
// Try to find TopLink 10.1.3 XMLSessionConfigLoader.
Class loaderClass = null;
Method getSessionMethod = null;
try {
loaderClass = Class.forName("oracle.toplink.tools.sessionconfiguration.XMLSessionConfigLoader");
getSessionMethod = SessionManager.class.getMethod("getSession",
new Class[] {loaderClass, String.class, ClassLoader.class, boolean.class, boolean.class, boolean.class});
if (logger.isDebugEnabled()) {
logger.debug("Using TopLink 10.1.3 XMLSessionConfigLoader");
}
}
catch (Exception ex) {
// TopLink 10.1.3 XMLSessionConfigLoader not found ->
// fall back to TopLink 9.0.4 XMLLoader.
if (logger.isDebugEnabled()) {
logger.debug("Using TopLink 9.0.4 XMLLoader");
}
XMLLoader loader = new XMLLoader(configLocation);
return (DatabaseSession) manager.getSession(loader, sessionName, sessionClassLoader, false, false);
}
// TopLink 10.1.3 XMLSessionConfigLoader found -> create loader instance
// through reflection and fetch specified Session from SessionManager.
// This invocation will check if the ClassLoader passed in is the same
// as the one used to a session currently loaded with the same "sessionName"
// If the ClassLoaders are different, then this LocalSessionFactory is being
// re-loaded after a hot-deploy and the existing DatabaseSession will be logged
// out and re-built from scratch.
try {
Constructor ctor = loaderClass.getConstructor(new Class[] {String.class});
Object loader = ctor.newInstance(new Object[] {configLocation});
return (DatabaseSession) getSessionMethod.invoke(manager,
new Object[] {loader, sessionName, sessionClassLoader, Boolean.FALSE, Boolean.FALSE, Boolean.TRUE});
}
catch (Exception ex) {
ReflectionUtils.handleReflectionException(ex);
throw new IllegalStateException("Should never get here");
}
}
/**
* Return the TopLink SessionManager to use for loading DatabaseSessions.
* <p>The default implementation creates a new plain SessionManager instance,
* leading to completely independent TopLink Session instances. Could be
* overridden to return a shared or pre-configured SessionManager.
*/
protected SessionManager getSessionManager() {
return new SessionManager();
}
/**
* Create a new SessionFactory for the given TopLink DatabaseSession.
* <p>The default implementation creates a ServerSessionFactory for a
* ServerSession and a SingleSessionFactory for a plain DatabaseSession.
* @param session the TopLink DatabaseSession to create a SessionFactory for
* @return the SessionFactory
* @throws TopLinkException in case of errors
* @see ServerSessionFactory
* @see SingleSessionFactory
* @see oracle.toplink.threetier.ServerSession
* @see oracle.toplink.sessions.DatabaseSession
*/
protected SessionFactory newSessionFactory(DatabaseSession session) {
if (session instanceof ServerSession) {
return new ServerSessionFactory((ServerSession) session);
}
else if (session instanceof SessionBroker) {
return new SessionBrokerSessionFactory((SessionBroker) session);
}
else {
return new SingleSessionFactory(session);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -