📄 sessions.xtp
字号:
<s1 title="Persistent and Distributed Sessions"><p>Sessions can be persistent across server restarts, includingapplication restarts when classes change. During development, for example,using file-based persistent sessions will let you work with a single sessioneven while you're modifying servlet classes.</p><s2 title='File Based'><ul><li>For single-server configurations<li>Useful in development when classes change often</ul><p>Persistent sessions are configured in the <var/web-app/>.File-based sessions use <var/file-store/>.</p><example><web-app> <session-config> <file-store>WEB-INF/sessions</file-store> </session-config></web-app></example><p>Sessions are stored as files in the <var/file-store/>directory. When the session changes, the updates will be written tothe file. After Resin loads an Application, it will load the storedsessions.</p><p>In general, file-based persistence is less useful in multi-serverenvironments. Although a network filesystem such as NFS will allow allthe servers to access the same filesystem, it's not designed for thefine-grained access. For example, NFS will cache pages. So if oneserver modifies the page, e.g. a session value, the other servers may not seethe change for several seconds.</p></s2><s2 title="Distributed Sessions"><p>Distributed sessions are intrinsically more complicated than single-serversessions. Single-server session can be implemented as a simple memory-basedHashtable. Distributed sessions must communicate between machines to ensurethe session state remains consistent.</p><p>Load balancing with multiple machines either uses <var/sticky sessions/> or<var/symmetrical sessions/>. Sticky sessions put more intelligence on theload balancer, and symmetrical sessions puts more intelligence on the JVMs.The choice of which to use depends on what kind of hardware you have,how many machines you're using and how you use sessions.</p><p>Distributed sessions can use a database as a backing store, or they candistribute the backup among all the servers using TCP.</p><s3 title="Symmetrical Sessions"><p>Symmetrical sessions happen with dumb load balancers like DNSround-robin. A single session may bounce from machine Ato machine B and back to machine B. For JDBC sessions, the symmetricalsession case needs the <var/always-load-session/> attribute described below.Each request must load the most up-to-date version of the session.</p><p>Distributed sessions in a symmetrical environment are required to makesessions work at all. Otherwise the state will end up spread across the JVMs.However, because each request must update its session information, it isless efficient than sticky sessions.</p></s3><s3 title="Sticky Sessions"><p>Sticky sessions require more intelligence on the load-balancer, butare easier for the JVM. Once a session starts, the load-balancer willalways send it to the same JVM. Resin's load balancing, for example, encodesthe session id as 'aaaXXX' and 'baaXXX'. The 'aaa' session will always goto JVM-a and 'baa' will always go to JVM-b.</p><p>Distributed sessions with a sticky session environment add reliability.If JVM-a goes down, JVM-b can pick up the session without the usernoticing any change. In addition, distributed sticky sessions are moreefficient. The distributor only needs to update sessions when they change.So if you update the session once when the user logs in, the distributedsessions can be very efficient.</p></s3><s3 title='always-load-session'><p>Symmetrical sessions must use the 'always-load-session' flag toupdate each session data on each request. always-load-session is onlyneeded for jdbc-store sessions. tcp-store sessions use a more-sophisticatedprotocol that eliminates the need for always-load-session, so tcp-storeignores the always-load-session flag.</p><p>The <var/always-load-session/> attribute forces sessions to check the store foreach request. By default, sessions are only loaded from persistentstore when they are created. In a configuration with multiple symmetricweb servers, sessions can be loaded on each request to ensure consistency.</p></s3><s3 title='always-save-session'><p>By default, Resin only saves session data when you add new valuesto the session object, i.e. if the request calls <var/setAttribute/>.This may be insufficient when storing large objects. For example, if youchange an internal field of a large object, Resin will not automaticallydetect that change and will not save the session object.</p><p>With <var/always-save-session/> Resin will always write the sessionto the store at the end of each request. Although this is less efficient,it guarantees that updates will get stored in the backup after eachrequest.</p></s3></s2><s2 title="TCP Distributed Sessions"><p>More sophisticated than the Database sessions is the TCP-ringdistributed sessions. With TCP distribution, the servers are arrangedin a ring. Each session belongs to a single JVM, say JVM-c. The nextJVM becomes the backup, e.g. JVM-d.</p><p>Because the storage is distributed across several machines, instead oftied to a single database server, the load is more fairly distributed. Also,because the sessions are distributed in a ring, there is no longer a singlepoint of failure.</p><p>The configuration is in the <var/web-app/>.</p><example><web-app> <session-config> <tcp-store/> </session-config></web-app></example><p>The <srun> and <srun-backup> hosts are treated as a ring. Eachhost will use the following host as a backup. When the session changes,the updates will be sent to the following host. When the host starts, itlooks up old sessions in the following host before using its own savedstate.</p><example title="Symmetric load-balanced servers"><http-server> <http id='a' port='80'/> <srun id='a' host='host-a' port='6802'/> <http id='b' port='80'/> <srun id='b' host='host-b' port='6802'/> <host id=''> <web-app id=''> <session-config> <tcp-store/> </session-config> </web-app> </host></http-server></example></s2><p>More details on the tcp-based sessions arein the <a href="../java_tut/tcp-sessions.xtp">TCP-sessions</a> page.</p><s2 title="Database Based"><p>Database backed sessions are the easiest to understand. Session datagets serialized and stored in a database. The advantage of database-backedsessions is it's simplicity. The disadvantage is that the database is oftenthe performance bottleneck of the system. By adding load to an already-loadedsystem, you may harm performance. One way around that bottleneck is to usea small, quick database like MySQL for your session store and save the "BigIron" database like Oracle for your core database needs.</p><p>The database must be specified using a <var/resource-ref/>.The database store will automaticallycreate a <var/session/> table.</p><example><web-app> <session-config> <jdbc-store>test</jdbc-store> <always-save-session/> </session-config></web-app></example><deftable><tr><td>data-source<td>data source name for the table<tr><td>table-name<td>database table for the session data<tr><td>blob-type<td>database type for a blob<tr><td>timestamp-type<td>database type for a blob<tr><td>session-timeout<td>cleanup time</deftable><example>CREATE TABLE persistent_session ( id VARCHAR(64) NOT NULL, data BLOB, mod_time TIMESTAMP, PRIMARY KEY(id))</example></s2></s1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -