Source Code Cross Referenced for DriverManagerDataSource.java in  » Search-Engine » compass-2.0 » org » apache » lucene » store » 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 » Search Engine » compass 2.0 » org.apache.lucene.store.jdbc.datasource 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004-2006 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.apache.lucene.store.jdbc.datasource;
018:
019:        import java.sql.Connection;
020:        import java.sql.DriverManager;
021:        import java.sql.SQLException;
022:
023:        /**
024:         * Simple implementation of the standard JDBC DataSource interface, configuring
025:         * a plain old JDBC Driver via bean properties, and returning a new Connection
026:         * for every <code>getConnection</code> call.
027:         * <p/>
028:         * Useful for test or standalone environments outside of a J2EE container.
029:         * Pool-assuming <code>Connection.close()</code> calls will simply close the
030:         * Connection, so any DataSource-aware persistence code should work.
031:         * <p/>
032:         * In a J2EE container, it is recommended to use a JNDI DataSource provided by
033:         * the container.
034:         * <p/>
035:         * If you need a "real" connection pool outside of a J2EE container, consider <a
036:         * href="http://jakarta.apache.org/commons/dbcp">Apache's Jakarta Commons DBCP</a>.
037:         * Its BasicDataSource is a full connection pool bean, supporting the same basic
038:         * properties as this class plus specific settings.
039:         * <p/>
040:         * Note, autoCommit property defaults to <code>false<code>.
041:         *
042:         * @author kimchy
043:         */
044:        public class DriverManagerDataSource extends AbstractDataSource {
045:
046:            private String driverClassName;
047:
048:            private String url;
049:
050:            private String username;
051:
052:            private String password;
053:
054:            private boolean autoCommit;
055:
056:            /**
057:             * Constructor for bean-style configuration.
058:             */
059:            public DriverManagerDataSource() {
060:            }
061:
062:            /**
063:             * Create a new DriverManagerDataSource with the given standard
064:             * DriverManager parameters.
065:             *
066:             * @param driverClassName the JDBC driver class name
067:             * @param url             the JDBC URL to use for accessing the DriverManager
068:             * @param username        the JDBC username to use for accessing the DriverManager
069:             * @param password        the JDBC password to use for accessing the DriverManager
070:             * @param autoCommit      the default autoCommit value that will be set to created connections
071:             * @see java.sql.DriverManager#getConnection(String, String, String)
072:             */
073:            public DriverManagerDataSource(String driverClassName, String url,
074:                    String username, String password, boolean autoCommit) {
075:                setDriverClassName(driverClassName);
076:                setUrl(url);
077:                setUsername(username);
078:                setPassword(password);
079:                setAutoCommit(autoCommit);
080:            }
081:
082:            /**
083:             * Create a new DriverManagerDataSource with the given standard
084:             * DriverManager parameters.
085:             * <p/>
086:             * Note, the autoCommit will default to <code>false</code>.
087:             *
088:             * @param url      the JDBC URL to use for accessing the DriverManager
089:             * @param username the JDBC username to use for accessing the DriverManager
090:             * @param password the JDBC password to use for accessing the DriverManager
091:             * @see java.sql.DriverManager#getConnection(String, String, String)
092:             */
093:            public DriverManagerDataSource(String url, String username,
094:                    String password) {
095:                setUrl(url);
096:                setUsername(username);
097:                setPassword(password);
098:            }
099:
100:            /**
101:             * Create a new DriverManagerDataSource with the given JDBC URL, not
102:             * specifying a username or password for JDBC access.
103:             *
104:             * @param url the JDBC URL to use for accessing the DriverManager
105:             * @see java.sql.DriverManager#getConnection(String)
106:             */
107:            public DriverManagerDataSource(String url) {
108:                setUrl(url);
109:            }
110:
111:            /**
112:             * Set the JDBC driver class name. This driver will get initialized on
113:             * startup, registering itself with the JDK's DriverManager.
114:             * <p/>
115:             * Alternatively, consider initializing the JDBC driver yourself before
116:             * instantiating this DataSource.
117:             *
118:             * @see Class#forName(String)
119:             * @see java.sql.DriverManager#registerDriver(java.sql.Driver)
120:             */
121:            public void setDriverClassName(String driverClassName) {
122:                this .driverClassName = driverClassName;
123:                try {
124:                    Class.forName(this .driverClassName, true, Thread
125:                            .currentThread().getContextClassLoader());
126:                } catch (ClassNotFoundException ex) {
127:                    throw new IllegalArgumentException(
128:                            "Could not load JDBC driver class ["
129:                                    + this .driverClassName + "]");
130:                }
131:            }
132:
133:            /**
134:             * Return the JDBC driver class name, if any.
135:             */
136:            public String getDriverClassName() {
137:                return driverClassName;
138:            }
139:
140:            /**
141:             * Set the JDBC URL to use for accessing the DriverManager.
142:             *
143:             * @see java.sql.DriverManager#getConnection(String, String, String)
144:             */
145:            public void setUrl(String url) {
146:                this .url = url;
147:            }
148:
149:            /**
150:             * Return the JDBC URL to use for accessing the DriverManager.
151:             */
152:            public String getUrl() {
153:                return url;
154:            }
155:
156:            /**
157:             * Set the JDBC username to use for accessing the DriverManager.
158:             *
159:             * @see java.sql.DriverManager#getConnection(String, String, String)
160:             */
161:            public void setUsername(String username) {
162:                this .username = username;
163:            }
164:
165:            /**
166:             * Return the JDBC username to use for accessing the DriverManager.
167:             */
168:            public String getUsername() {
169:                return username;
170:            }
171:
172:            /**
173:             * Set the JDBC password to use for accessing the DriverManager.
174:             *
175:             * @see java.sql.DriverManager#getConnection(String, String, String)
176:             */
177:            public void setPassword(String password) {
178:                this .password = password;
179:            }
180:
181:            /**
182:             * Return the JDBC password to use for accessing the DriverManager.
183:             */
184:            public String getPassword() {
185:                return password;
186:            }
187:
188:            /**
189:             * Returns the auto commit setting that a connection will be created
190:             */
191:            public boolean getAutoCommit() {
192:                return this .autoCommit;
193:            }
194:
195:            /**
196:             * Sets the auto commit setting that a connection will be created
197:             */
198:            public void setAutoCommit(boolean autoCommit) {
199:                this .autoCommit = autoCommit;
200:            }
201:
202:            /**
203:             * This implementation delegates to
204:             * <code>getConnectionFromDriverManager</code>, using the default
205:             * username and password of this DataSource.
206:             *
207:             * @see #getConnectionFromDriverManager()
208:             */
209:            public Connection getConnection() throws SQLException {
210:                return getConnectionFromDriverManager();
211:            }
212:
213:            /**
214:             * This implementation delegates to
215:             * <code>getConnectionFromDriverManager</code>, using the given username
216:             * and password.
217:             *
218:             * @see #getConnectionFromDriverManager(String, String, String)
219:             */
220:            public Connection getConnection(String username, String password)
221:                    throws SQLException {
222:                return getConnectionFromDriverManager(getUrl(), username,
223:                        password);
224:            }
225:
226:            /**
227:             * Get a Connection from the DriverManager, using the default username and
228:             * password of this DataSource.
229:             *
230:             * @see #getConnectionFromDriverManager(String, String, String)
231:             */
232:            protected Connection getConnectionFromDriverManager()
233:                    throws SQLException {
234:                return getConnectionFromDriverManager(getUrl(), getUsername(),
235:                        getPassword());
236:            }
237:
238:            /**
239:             * Getting a connection using the nasty static from DriverManager is
240:             * extracted into a protected method to allow for easy unit testing.
241:             * <p/>
242:             * Note, that it sets the auto commit to false
243:             *
244:             * @see java.sql.DriverManager#getConnection(String, String, String)
245:             */
246:            protected Connection getConnectionFromDriverManager(String url,
247:                    String username, String password) throws SQLException {
248:
249:                Connection conn = DriverManager.getConnection(url, username,
250:                        password);
251:                if (conn.getAutoCommit() != autoCommit) {
252:                    conn.setAutoCommit(autoCommit);
253:                }
254:                return conn;
255:            }
256:
257:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.