The following sections describe how to set up sessions and session persistence:
Session tracking enables you to track a user's progress over multiple servlets or HTML pages, which, by nature, are stateless. A session is defined as a series of related browser requests that come from the same client during a certain time period. Session tracking ties together a series of browser requests—think of these requests as pages—that may have some meaning as a whole, such as a shopping cart application.
WebLogic Server is set up to handle session tracking by default. You need not set any of these properties to use session tracking. However, configuring how WebLogic Server manages sessions is a key part of tuning your application for best performance. Tuning depends upon factors such as:
You configure WebLogic Server session tracking with properties in the WebLogic-specific deployment descriptor, weblogic.xml. For instructions on editing the WebLogic-specific deployment descriptor, see Step 4: Define session parameters.
For a complete list of session attributes, see jsp-descriptor.
WebLogic Server 7.0 introduced a change to the SessionID format that caused some load balancers to lose the ability to retain session stickiness.
A new server startup flag, -Dweblogic.servlet.useExtendedSessionFormat=true , retains the information that the load-balancing application needs for session stickiness. The extended session ID format will be part of the URL if URL rewriting is activated, and the startup flag is set to true.
You can specify an interval of time after which HTTP sessions expire. When a session expires, all data stored in the session is discarded. You can set the interval in either web.xml or weblogic.xml:
WebLogic Server uses cookies for session management when supported by the client browser.
The cookies that WebLogic Server uses to track sessions are set as transient by default and do not outlive the session. When a user quits the browser, the cookies are lost and the session lifetime is regarded as over. This behavior is in the spirit of session usage and it is recommended that you use sessions in this way.
You can configure session-tracking attributes of cookies in the WebLogic-specific deployment descriptor, weblogic.xml. A complete list of session and cookie-related attributes is available jsp-descriptor.
For instructions on editing the WebLogic-specific deployment descriptor, see Step 4: Define session parameters.
Using Cookies That Outlive a Session
For longer-lived client-side user data, your application should create and set its own cookies on the browser via the HTTP servlet API, and should not attempt to use the cookies associated with the HTTP session. Your application might use cookies to auto-login a user from a particular machine, in which case you would set a new cookie to last for a long time. Remember that the cookie can only be sent from that client machine. Your application should store data on the server if it must be accessed by the user from multiple locations.
You cannot directly connect the age of a browser cookie with the length of a session. If a cookie expires before its associated session, that session becomes orphaned. If a session expires before its associated cookie, the servlet is not be able to find a session. At that point, a new session is assigned when the request.getSession(true) method is called. You should only make transient use of sessions.
You can set the maximum life of a cookie with the CookieMaxAgeSecs parameter in the session descriptor of the weblogic.xml deployment descriptor. For more information, see Step 4: Define session parameters
Logging Out and Ending a Session
User authentication information is stored both in the user's session data and in the context of a server or virtual host that is targeted by a Web Application. The session.invalidate() method, which is often used to log out a user, only invalidates the current session for a user—the user's authentication information still remains valid and is stored in the context of the server or virtual host. If the server or virtual host is hosting only one Web Application, the session.invalidate() method, in effect, logs out the user.
There are several Java methods and strategies you can use when using authentication with multiple Web Applications. For more information, see Implementing Single Sign-On in the Programming WebLogic HTTP Servlets,.
Configuring Session Persistence
Use Session Persistence to permanently stored data from an HTTP session object in order to enable failover and load balancing across a cluster of WebLogic Servers. When your applications stores data in an HTTP session object, the data must be serializable.
There are five different implementations of session persistence:
The first four are discussed here; in-memory replication is discussed in in Using WebLogic Server Clusters.
File, JDBC, Cookie-based, and memory (single-server, non-populated) session persistence have some common properties. Each persistence method has its own set of attributes, as discussed in the following sections.
Common Properties of Session Attributes
This section describes attributes common to file system, memory (single-server, non-replicated), JDBC, and cookie-based persistence. You can configure the number of sessions that are held in memory by setting the following properties in the <session-descriptor> element of the WebLogic-specific deployment descriptor, weblogic.xml. These properties are only applicable if you are using session persistence:
Using Memory-based, Single-server, Non-replicated Persistent Storage
To use memory-based, single-server, non-replicated persistent storage, set the PersistentStoreType property in the <session-descriptor> element of the WebLogic-specific deployment descriptor, weblogic.xml to memory. When you use memory-based storage, all session information is stored in memory and is lost when you stop and restart WebLogic Server.
Note: If you do not allocate sufficient heap size when running WebLogic Server, your server may run out of memory under heavy load.
Using File-based Persistent Storage
To configure file-based persistent storage for sessions:
If you do not explicitly set a value for this attribute, a temporary directory is created for you by WebLogic Server.
If you are using file-based persistence in a cluster, you must explicitly set this attribute to a shared directory that is accessible to all the servers in a cluster. You must create this directory yourself.
Using a Database for Persistent Storage (JDBC persistence)
JDBC persistence stores session data in a database table using a schema provided for this purpose. You can use any database for which you have a JDBC driver. You configure database access by using connection pools.
To configure JDBC-based persistent storage for sessions:
For more details on setting up a database connection pool, see Managing JDBC Connectivity.
If you are using an Oracle DBMS, use the following SQL statement to create the wl_servlet_sessions table:
create table wl_servlet_sessions
( wl_id VARCHAR2(100) NOT NULL,
wl_context_path VARCHAR2(100) NOT NULL,
wl_is_new CHAR(1),
wl_create_time NUMBER(20),
wl_is_valid CHAR(1),
wl_session_values LONG RAW,
wl_access_time NUMBER(20),
wl_max_inactive_interval INTEGER,
PRIMARY KEY (wl_id, wl_context_path) );
If you are using SqlServer2000, use the following SQL statement to create the wl_servlet_sessions table:
create table wl_servlet_sessions
( wl_id VARCHAR2(100) NOT NULL,
wl_context_path VARCHAR2(100) NOT NULL,
wl_is_new VARCHAR(1),
wl_create_time DeCIMAL,
wl_is_valid VARCHAR(1),
wl_session_values IMAGE,
wl_access_time DECIMAL,
wl_max_inactive_interval INTEGER,
PRIMARY KEY (wl_id, wl_context_path) );
Modify one of the preceeding SQL statements for use with your DBMS.
Note: You can configure a maximum duration that JDBC session persistence should wait for a JDBC connection from the connection pool before failing to load the session data with the JDBCConnectionTimeoutSecs attribute. For more information, see JDBCConnectionTimeoutSecs.
Using Cookie-Based Session Persistence
Cookie-based session persistence provides a stateless solution for session persistence by storing all session data in a cookie that is stored in the user's browser. Cookie-based session persistence is most useful when you do not need to store large amounts of data in the session. Cookie-based session persistence can make managing your WebLogic Server installation easier because clustering failover logic is not required. Because the session is stored in the browser, not on the server, you can start and stop WebLogic Servers without losing sessions.
There are some limitations to cookie-based session persistence:
To set up cookie-based session persistence:
In some situations, a browser or wireless device may not accept cookies, which makes session tracking using cookies impossible. URL rewriting is a solution to this situation that can be substituted automatically when WebLogic Server detects that the browser does not accept cookies. URL rewriting involves encoding the session ID into the hyper-links on the Web pages that your servlet sends back to the browser. When the user subsequently clicks these links, WebLogic Server extracts the ID from the URL address and finds the appropriate HttpSession when your servlet calls the getSession() method.
Enable URL rewriting in WebLogic Server by setting the URLRewritingEnabled attribute in the WebLogic-specific deployment descriptor, weblogic.xml, under the <session-descriptor> element.The default value for this attribute is true. See URLRewritingEnabled.
Coding Guidelines for URL Rewriting
There are some general guidelines for how your code should handle URLs in order to support URL rewriting.
out.println("<a href=\"/myshop/catalog.jsp\">catalog</a>");
Instead, use the HttpServletResponse.encodeURL() method, for example:
out.println("<a href=\""
+ response.encodeURL("myshop/catalog.jsp")
+ "\">catalog</a>");
Calling the encodeURL() method determines if the URL needs to be rewritten, and if so, it rewrites it by including the session ID in the URL. The session ID is appended to the URL and begins with a semicolon.
if (session.isNew())
response.sendRedirect (response.encodeRedirectUrl(welcomeURL));
URL Rewriting and Wireless Access Protocol (WAP)
If you are writing a WAP application, you must use URL rewriting because the WAP protocol does not support cookies. In addition, some WAP devices have a 128-character limit on the length of a URL (including parameters), which limits the amount of data that can be transmitted using URL rewriting. To allow more space for parameters, you can limit the size of the session ID that is randomly generated by WebLogic Server. See IDLength.
选自<<_NHibernateQJava对象持久化技术详?gt;> 作者:孙卫?/FONT> 来源:www.javathinker.org
如果转蝲Q请标明出处Q谢?BR>
1.1 Hibernate API 变化
1.1.1 包名
1.1.2 org.hibernate.classic?
1.1.3 Hibernate所依赖的第三方软g?/FONT>
1.1.4 异常模型
1.1.5 Session接口
1.1.6 createSQLQuery()
1.1.7 Lifecycle ?Validatable 接口
1.1.8 Interceptor接口
1.1.9 UserType和CompositeUserType接口
1.1.10 FetchModec?
1.1.11 PersistentEnumc?
1.1.12 对Blob 和Clob的支?
1.1.13 Hibernate中供扩展的API的变?
1.2 元数据的变化
1.2.1 索策?
1.2.2 对象标识W的映射
1.2.3 集合映射
1.2.4 DTD
1.3 查询语句的变?
1.3.1 indices()和elements()函数
管Hibernate 3.0 与Hibernate2.1的源代码是不兼容的,但是当Hibernate开发小l在设计Hibernate3.0Ӟ为简化升UHibernate版本作了周到的考虑。对于现有的ZHibernate2.1的Java目Q可以很方便的把它升U到Hibernate3.0?BR>
本文描述了Hibernate3.0版本的新变化QHibernate3.0版本的变化包括三个方面:
Q?QAPI的变化,它将影响到JavaE序代码?BR>Q?Q元数据Q它媄响到对象-关系映射文g?BR>Q?QHQL查询语句?BR>
值得注意的是Q?Hibernate3.0q不会完全取代Hibernate2.1。在同一个应用程序中Q允许Hibernate3.0和Hibernate2.1q存?BR>
1.1 Hibernate API 变化
1.1.1 包名
Hibernate3.0的包的根路径? “org.hibernate?Q而在Hibernate2.1中ؓ“net.sf.hibernate”。这一命名变化使得Hibernate2.1和Hibernate3.0能够同时在同一个应用程序中q行?BR>
如果希望把已有的应用升到Hibernate3.0Q那么升U的W一步是把Java源程序中的所有“net.sf.hibernate”替换ؓ“org.hibernate”?BR>
Hibernate2.1中的“net.sf.hibernate.expression”包被改名ؓ“org.hibernate.criterion”。假如应用程序用了Criteria APIQ那么在升的过E中Q必LJava源程序中的所有“net.sf.hibernate.expression”替换ؓ“org.hibernate.criterion”?BR>
如果应用使用了除Hibernate以外的其他外部YӞ而这个外部Y件又引用了Hibernate的接口,那么在升U时必须十分心。例如EHCache拥有自己的CacheProviderQ?net.sf.ehcache.hibernate.ProviderQ在q个cM引用了Hibernate2.1中的接口Q在升应用Ӟ可以采用以下办法之一来升UEHCache:
Q?Q手工修改net.sf.ehcache.hibernate.Providerc,使它引用Hibernate3.0中的接口?BR>Q?Q等到EHCache软g本n升Z用Hibernate3.0后,使用新的EHCache软g?BR>Q?Q用Hibernate3.0中内|的CacheProviderQorg.hibernate.cache.EhCacheProvider?BR>
1.1.2 org.hibernate.classic?BR>
Hibernate3.0把一些被废弃的接口都转移到org.hibernate.classic中?BR>
1.1.3 Hibernate所依赖的第三方软g?BR>
在Hibernate3.0的Y件包的lib目录下的README.txt文g中,描述了Hibernate3.0所依赖的第三方软g包的变化?BR>
1.1.4 异常模型
在Hibernate3.0中,HibernateException异常以及它的所有子c都l承了java.lang.RuntimeException。因此在~译Ӟ~译器不会再查HibernateException?BR>
1.1.5 Session接口
在Hibernate3.0中,原来Hibernate2.1的Session接口中的有些基本Ҏ也被废弃Q但Z化升U,q些Ҏ依然是可用的Q可以通过org.hibernate.classic.Session子接口来讉K它们Q例如:
org.hibernate.classic.Session session=sessionFactory.openSession();
session.delete("delete from Customer ");
在Hibernate3.0中,org.hibernate.classic.Session接口l承了org.hibernate.Session接口Q在org.hibernate.classic.Session接口中包含了一pd被废弃的ҎQ如find()、interate(){。SessionFactory接口的openSession()Ҏq回org.hibernate.classic.Sessioncd的实例。如果希望在E序中完全用Hibernate3.0Q可以采用以下方式创建Session实例Q?BR>
org.hibernate.Session session=sessionFactory.openSession();
如果是对已有的程序进行简单的升Qƈ且希望仍然调用Hibernate2.1中Session的一些接口,可以采用以下方式创徏Session实例Q?BR>
org.hibernate.classic.Session session=sessionFactory.openSession();
在Hibernate3.0中,Session接口中被废弃的方法包括:
* 执行查询的方法:find()、iterate()、filter()和delete(String hqlSelectQuery)
* saveOrUpdateCopy()
Hibernate3.0一律采用createQuery()Ҏ来执行所有的查询语句Q采用DELETE 查询语句来执行批量删除,采用merge()Ҏ来替?saveOrUpdateCopy()Ҏ?/P>
提示Q在Hibernate2.1中,Session的delete()Ҏ有几U重载Ş式,其中参数为HQL查询语句的delete()Ҏ在Hibernate3.0中被废弃Q而参CؓOjbectcd的的delete()Ҏ依然被支持。delete(Object o)Ҏ用于删除参数指定的对象,该方法支持联删除?/I>
Hibernate2.1没有Ҏ量更新和扚w删除提供很好的支持,参见<<_NHibernate>>一书的W?3章的13.1.1节(扚w更新和批量删除)Q而Hibernate3.0Ҏ量更新和扚w删除提供了支持,能够直接执行扚w更新或批量删除语句,无需把被更新或删除的对象先加载到内存中。以下是通过Hibernate3.0执行扚w更新的程序代码:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
String hqlUpdate = "update Customer set name = :newName where name = :oldName";
int updatedEntities = s.createQuery( hqlUpdate )
.setString( "newName", newName )
.setString( "oldName", oldName )
.executeUpdate();
tx.commit();
session.close();
以下是通过Hibernate3.0执行扚w删除的程序代码:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
String hqlDelete = "delete Customer where name = :oldName";
int deletedEntities = s.createQuery( hqlDelete )
.setString( "oldName", oldName )
.executeUpdate();
tx.commit();
session.close();
1.1.6 createSQLQuery()
在Hibernate3.0中,Session接口的createSQLQuery()Ҏ被废弃,被移到org.hibernate.classic.Session接口中。Hibernate3.0采用新的SQLQuery接口来完成相同的功能?BR>
1.1.7 Lifecycle ?Validatable 接口
Lifecycle和Validatable 接口被废弃,q且被移到org.hibernate.classic包中?BR>
1.1.8 Interceptor接口
在Interceptor 接口中加入了两个新的Ҏ?用户创徏的Interceptor实现cd升的过E中Q需要ؓq两个新Ҏ提供Ҏ体ؓI的实现。此外,instantiate()Ҏ的参C了修改,isUnsaved()Ҏ被改名ؓisTransient()?BR>
1.1.9 UserType和CompositeUserType接口
在UserType和CompositeUserType接口中都加入了一些新的方法,q两个接口被Udorg.hibernate.usertype包中Q用户定义的UserType和CompositeUserType实现cdd现这些新Ҏ?/P>
Hibernate3.0提供了ParameterizedType接口Q用于更好的重用用户自定义的cd?/P>
1.1.10 FetchModec?BR>
FetchMode.LAZY ?FetchMode.EAGER被废弃。取而代之的分别为FetchMode.SELECT 和FetchMode.JOIN?BR>
1.1.11 PersistentEnumc?BR>
PersistentEnum被废弃ƈ删除。已l存在的应用应该采用UserType来处理枚丄型?BR>
1.1.12 对Blob 和Clob的支?BR>
Hibernate对Blob和Clob实例q行了包装,使得那些拥有Blob或Clobcd的属性的cȝ实例可以被游R序列化或反序列化,以及传递到merge()Ҏ中?BR>
1.1.13 Hibernate中供扩展的API的变?BR>
org.hibernate.criterion?org.hibernate.mapping?org.hibernate.persister和org.hibernate.collection 包的l构和实现发生了重大的变化。多数基于Hibernate
2.1 的应用不依赖于这些包Q因此不会被影响。如果你的应用扩展了q些包中的类Q那么必非常小心的对受影响的程序代码进行升U?BR>
1.2 元数据的变化
1.2.1 索策?BR>
在Hibernate2.1中,lazy属性的默认gؓ“false”,而在Hibernate3.0中,lazy属性的默认gؓ“true”。在升映射文gӞ如果原来的映文件中的有兛_素,?lt;set>?lt;class>{没有显式设|lazy属性,那么必须把它们都昑ּ的设|ؓlazy=“true”。如果觉得这U升U方式很ȝQ可以采取另一单的升方式Q在<hibernate-mapping>元素中设|? default-lazy=“false”?
1.2.2 对象标识W的映射
unsaved-value属性是可选的Q在多数情况下,Hibernate3.0把unsaved-value="0" 作ؓ默认倹{?BR>
在Hibernate3.0中,当用自然主键和游离对象Ӟ不再实现Interceptor.isUnsaved()Ҏ?如果没有讄q个ҎQ当Hibernate3.0无法区分对象的状态时Q会查询数据库,来判断这个对象到底是临时对象Q还是游d象。不q,昑ּ的用Interceptor.isUnsaved()Ҏ会获得更好的性能Q因可以减少Hibernate直接讉K数据库的ơ数?BR>
1.2.3 集合映射
<index>元素在某些情况下?lt;list-index>?lt;map-key>元素替代。此外,Hibernate3.0?lt;map-key-many-to-many> 元素来替代原来的<key-many-to-many>.元素Q用<composite-map-key>元素来替代原来的<composite-index>元素?BR>
1.2.4 DTD
对象-关系映射文g中的DTD文档Q由原来的:
http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd
改ؓQ?BR>http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd
1.3 查询语句的变?BR>
Hibernate3.0 采用新的ZANTLR的HQL/SQL查询译器,不过QHibernate2.1的查询翻译器也依然存在。在Hibernate的配|文件中Qhibernate.query.factory_class属性用来选择查询译器。例如:
Q?Q选择Hibernate3.0的查询翻译器Q?BR>hibernate.query.factory_class= org.hibernate.hql.ast.ASTQueryTranslatorFactory
Q?Q选择Hibernate2.1的查询翻译器
hibernate.query.factory_class= org.hibernate.hql.classic.ClassicQueryTranslatorFactory
提示QANTLR是用UJava语言~写出来的一个编译工P它可生成Java语言或者是C++的词法和语法分析器,q可产生语法分析树ƈ对该树进行遍历。ANTLR׃是纯Java的,因此可以安装在Q意^CQ但是需要JDK的支持?/I>
Hibernate开发小l尽力保证Hibernate3.0的查询翻译器能够支持Hibernate2.1的所有查询语句。不q,对于许多已经存在的应用,在升U过E中Q也不妨仍然使用Hibernate2.1的查询翻译器?BR>值得注意的是Q?Hibernate3.0的查询翻译器存在一个BugQ不支持某些theta-styleq结查询方言Q如Oracle8i的OracleDialect方言、Sybase11Dialect。解册一问题的办法有两种Q(1Q改Z用支持ANSI-styleq结查询的方aQ如 Oracle9Dialect,Q?Q如果升U的时候遇到这一问题Q那么还是改Z用Hibernate2.1的查询翻译器?BR>
1.3.1 indices()和elements()函数
在HQL的select子句中废弃了indices()和elements()函数Q因两个函数的语法很让用戯解,可以用显式的q接查询语句来替?select elements(...) 。而在HQL的where子句中,仍然可以使用elements()函数?BR>