Source Code Cross Referenced for IsolationLevelDataSourceAdapter.java in  » J2EE » spring-framework-2.5 » org » springframework » jdbc » datasource » 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 » J2EE » spring framework 2.5 » org.springframework.jdbc.datasource 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2002-2007 the original author or authors.
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:
017:        package org.springframework.jdbc.datasource;
018:
019:        import java.sql.Connection;
020:        import java.sql.SQLException;
021:
022:        import org.springframework.core.Constants;
023:        import org.springframework.transaction.TransactionDefinition;
024:        import org.springframework.transaction.support.DefaultTransactionDefinition;
025:        import org.springframework.transaction.support.TransactionSynchronizationManager;
026:
027:        /**
028:         * An adapter for a target {@link javax.sql.DataSource}, applying the current
029:         * Spring transaction's isolation level (and potentially specified user credentials)
030:         * to every <code>getConnection</code> call. Also applies the read-only flag,
031:         * if specified.
032:         *
033:         * <p>Can be used to proxy a target JNDI DataSource that does not have the
034:         * desired isolation level (and user credentials) configured. Client code
035:         * can work with this DataSource as usual, not worrying about such settings.
036:         *
037:         * <p>Inherits the capability to apply specific user credentials from its superclass
038:         * {@link UserCredentialsDataSourceAdapter}; see the latter's javadoc for details
039:         * on that functionality (e.g. {@link #setCredentialsForCurrentThread}).
040:         *
041:         * <p><b>WARNING:</b> This adapter simply calls
042:         * {@link java.sql.Connection#setTransactionIsolation} and/or
043:         * {@link java.sql.Connection#setReadOnly} for every Connection obtained from it.
044:         * It does, however, <i>not</i> reset those settings; it rather expects the target
045:         * DataSource to perform such resetting as part of its connection pool handling.
046:         * <b>Make sure that the target DataSource properly cleans up such transaction state.</b>
047:         *
048:         * @author Juergen Hoeller
049:         * @since 2.0.3
050:         * @see #setIsolationLevel
051:         * @see #setIsolationLevelName
052:         * @see #setUsername
053:         * @see #setPassword
054:         */
055:        public class IsolationLevelDataSourceAdapter extends
056:                UserCredentialsDataSourceAdapter {
057:
058:            /** Constants instance for TransactionDefinition */
059:            private static final Constants constants = new Constants(
060:                    TransactionDefinition.class);
061:
062:            private Integer isolationLevel;
063:
064:            /**
065:             * Set the default isolation level by the name of the corresponding constant
066:             * in {@link org.springframework.transaction.TransactionDefinition}, e.g.
067:             * "ISOLATION_SERIALIZABLE".
068:             * <p>If not specified, the target DataSource's default will be used.
069:             * Note that a transaction-specific isolation value will always override
070:             * any isolation setting specified at the DataSource level.
071:             * @param constantName name of the constant
072:             * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_UNCOMMITTED
073:             * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_COMMITTED
074:             * @see org.springframework.transaction.TransactionDefinition#ISOLATION_REPEATABLE_READ
075:             * @see org.springframework.transaction.TransactionDefinition#ISOLATION_SERIALIZABLE
076:             * @see #setIsolationLevel
077:             */
078:            public final void setIsolationLevelName(String constantName)
079:                    throws IllegalArgumentException {
080:                if (constantName == null
081:                        || !constantName
082:                                .startsWith(DefaultTransactionDefinition.PREFIX_ISOLATION)) {
083:                    throw new IllegalArgumentException(
084:                            "Only isolation constants allowed");
085:                }
086:                setIsolationLevel(constants.asNumber(constantName).intValue());
087:            }
088:
089:            /**
090:             * Specify the default isolation level to use for Connection retrieval,
091:             * according to the JDBC {@link java.sql.Connection} constants
092:             * (equivalent to the corresponding Spring
093:             * {@link org.springframework.transaction.TransactionDefinition} constants).
094:             * <p>If not specified, the target DataSource's default will be used.
095:             * Note that a transaction-specific isolation value will always override
096:             * any isolation setting specified at the DataSource level.
097:             * @see java.sql.Connection#TRANSACTION_READ_UNCOMMITTED
098:             * @see java.sql.Connection#TRANSACTION_READ_COMMITTED
099:             * @see java.sql.Connection#TRANSACTION_REPEATABLE_READ
100:             * @see java.sql.Connection#TRANSACTION_SERIALIZABLE
101:             * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_UNCOMMITTED
102:             * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_COMMITTED
103:             * @see org.springframework.transaction.TransactionDefinition#ISOLATION_REPEATABLE_READ
104:             * @see org.springframework.transaction.TransactionDefinition#ISOLATION_SERIALIZABLE
105:             * @see org.springframework.transaction.TransactionDefinition#getIsolationLevel()
106:             * @see org.springframework.transaction.support.TransactionSynchronizationManager#getCurrentTransactionIsolationLevel()
107:             */
108:            public void setIsolationLevel(int isolationLevel) {
109:                if (!constants.getValues(
110:                        DefaultTransactionDefinition.PREFIX_ISOLATION)
111:                        .contains(new Integer(isolationLevel))) {
112:                    throw new IllegalArgumentException(
113:                            "Only values of isolation constants allowed");
114:                }
115:                this .isolationLevel = (isolationLevel != TransactionDefinition.ISOLATION_DEFAULT ? new Integer(
116:                        isolationLevel)
117:                        : null);
118:            }
119:
120:            /**
121:             * Return the statically specified isolation level,
122:             * or <code>null</code> if none.
123:             */
124:            protected Integer getIsolationLevel() {
125:                return this .isolationLevel;
126:            }
127:
128:            /**
129:             * Applies the current isolation level value and read-only flag
130:             * to the returned Connection.
131:             * @see #getCurrentIsolationLevel()
132:             * @see #getCurrentReadOnlyFlag()
133:             */
134:            protected Connection doGetConnection(String username,
135:                    String password) throws SQLException {
136:                Connection con = super .doGetConnection(username, password);
137:                Boolean readOnlyToUse = getCurrentReadOnlyFlag();
138:                if (readOnlyToUse != null) {
139:                    con.setReadOnly(readOnlyToUse.booleanValue());
140:                }
141:                Integer isolationLevelToUse = getCurrentIsolationLevel();
142:                if (isolationLevelToUse != null) {
143:                    con.setTransactionIsolation(isolationLevelToUse.intValue());
144:                }
145:                return con;
146:            }
147:
148:            /**
149:             * Determine the current isolation level: either the transaction's
150:             * isolation level or a statically defined isolation level.
151:             * @return the current isolation level, or <code>null</code> if none
152:             * @see org.springframework.transaction.support.TransactionSynchronizationManager#getCurrentTransactionIsolationLevel()
153:             * @see #setIsolationLevel
154:             */
155:            protected Integer getCurrentIsolationLevel() {
156:                Integer isolationLevelToUse = TransactionSynchronizationManager
157:                        .getCurrentTransactionIsolationLevel();
158:                if (isolationLevelToUse == null) {
159:                    isolationLevelToUse = getIsolationLevel();
160:                }
161:                return isolationLevelToUse;
162:            }
163:
164:            /**
165:             * Determine the current read-only flag: by default,
166:             * the transaction's read-only hint.
167:             * @return whether there is a read-only hint for the current scope
168:             * @see org.springframework.transaction.support.TransactionSynchronizationManager#isCurrentTransactionReadOnly()
169:             */
170:            protected Boolean getCurrentReadOnlyFlag() {
171:                boolean txReadOnly = TransactionSynchronizationManager
172:                        .isCurrentTransactionReadOnly();
173:                return (txReadOnly ? Boolean.TRUE : null);
174:            }
175:
176:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.