Source Code Cross Referenced for SessionInterceptingSessionFactory.java in  » Testing » unitils » org » unitils » hibernate » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » unitils » org.unitils.hibernate.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2006-2007,  Unitils.org
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *     http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package org.unitils.hibernate.util;
017:
018:        import java.sql.Connection;
019:        import java.util.HashSet;
020:        import java.util.Set;
021:
022:        import org.apache.commons.logging.Log;
023:        import org.apache.commons.logging.LogFactory;
024:        import org.hibernate.HibernateException;
025:        import org.hibernate.Interceptor;
026:        import org.hibernate.SessionFactory;
027:        import org.hibernate.classic.Session;
028:        import org.unitils.core.Unitils;
029:        import org.unitils.database.DatabaseModule;
030:        import org.unitils.hibernate.HibernateModule;
031:
032:        /**
033:         * A wrapper for a Hibernate session factory that intercepts all opened session. These sessions can
034:         * later be retrieved for e.g. closing them or flushing their updates to the database.
035:         *
036:         * @author Filip Neven
037:         * @author Tim Ducheyne
038:         */
039:        public class SessionInterceptingSessionFactory extends
040:                BaseSessionInterceptingSessionFactoryProxy {
041:
042:            /* The logger instance for this class */
043:            private static Log logger = LogFactory
044:                    .getLog(SessionInterceptingSessionFactory.class);
045:
046:            /**
047:             * The intercepted sessions.
048:             */
049:            protected Set<org.hibernate.Session> sessions = new HashSet<org.hibernate.Session>();
050:
051:            /**
052:             * Creates a wrapper for the given session factory.
053:             *
054:             * @param sessionFactory The factory, not null
055:             */
056:            public SessionInterceptingSessionFactory(
057:                    SessionFactory sessionFactory) {
058:                super (sessionFactory);
059:            }
060:
061:            /**
062:             * Opens a new hibernate session. Overriden to store the opened session.
063:             *
064:             * @return the session, not null
065:             */
066:            @Override
067:            public Session openSession() throws HibernateException {
068:                Session session = super .openSession();
069:                sessions.add(session);
070:                return session;
071:            }
072:
073:            /**
074:             * Opens a new hibernate session. Overriden to store the opened session.
075:             *
076:             * @param connection The connection to use
077:             * @return the session, not null
078:             */
079:            @Override
080:            public Session openSession(Connection connection) {
081:                Session session = super .openSession(connection);
082:                sessions.add(session);
083:                return session;
084:            }
085:
086:            /**
087:             * Opens a new hibernate session. Overriden to store the opened session.
088:             *
089:             * @param connection  The connection to use
090:             * @param interceptor The session interceptor to use
091:             * @return the session, not null
092:             */
093:            @Override
094:            public Session openSession(Connection connection,
095:                    Interceptor interceptor) {
096:                Session session = super .openSession(connection, interceptor);
097:                sessions.add(session);
098:                return session;
099:            }
100:
101:            /**
102:             * Opens a new hibernate session. Overriden to store the opened session.
103:             *
104:             * @param interceptor The session interceptor to use
105:             * @return the session, not null
106:             */
107:            @Override
108:            public Session openSession(Interceptor interceptor)
109:                    throws HibernateException {
110:                Session session = super .openSession(interceptor);
111:                sessions.add(session);
112:                return session;
113:            }
114:
115:            /**
116:             * Gets the current session if <code>CurrentSessionContext</code> is configured.
117:             *
118:             * @return The current session
119:             */
120:            @Override
121:            public Session getCurrentSession() throws HibernateException {
122:                Session session = super .getCurrentSession();
123:                sessions.add(session);
124:                return session;
125:            }
126:
127:            /**
128:             * Gets all open intercepted sessions.
129:             *
130:             * @return The sessions, not null
131:             */
132:            public Set<org.hibernate.Session> getOpenedSessions() {
133:                return sessions;
134:            }
135:
136:            /**
137:             * Closes and clears all open sessions.
138:             */
139:            public void closeOpenSessions() {
140:                for (org.hibernate.Session session : sessions) {
141:                    if (session.isOpen()) {
142:                        logger.info("Closing hibernate session " + session);
143:                        session.close();
144:                    }
145:                }
146:                clearInterceptedSessions();
147:            }
148:
149:            /**
150:             * Flushes all open sessions.
151:             */
152:            public void flushOpenSessions() {
153:                for (org.hibernate.Session session : sessions) {
154:                    if (session.isOpen()) {
155:                        logger.info("Flushing hibernate session " + session);
156:                        session.flush();
157:                    }
158:                }
159:            }
160:
161:            /**
162:             * Removes all intercepted sessions
163:             */
164:            public void clearInterceptedSessions() {
165:                sessions.clear();
166:            }
167:
168:            protected HibernateModule getHibernateModule() {
169:                return Unitils.getInstance().getModulesRepository()
170:                        .getModuleOfType(HibernateModule.class);
171:            }
172:
173:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.