Java Doc for ConnectionSingleton.java in  » Content-Management-System » TransferCM » com » methodhead » persistable » 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 » Content Management System » TransferCM » com.methodhead.persistable 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   com.methodhead.persistable.ConnectionSingleton

ConnectionSingleton
public class ConnectionSingleton (Code)

A singleton that provides JDBC connections using the Jakarta Commons DBCP component. Use com.methodhead.persistable.ConnectionSingleton.init(java.lang.Stringjava.util.Properties)init() to initialize the singleton. The following properties are expected:

  • driver: The JDBC driver.
  • uri: The database URI.
  • user: The database login.
  • password: The database password.
  • validQuery (optional): The query used to verify the health of a connection (defaults to SELECT 1).
  • evictionPeriodMillis (optional): The time between connection health tests (defaults to 3600000, 1 hour).
  • maxActive (optional): The maximum number of connections (defaults to 8).
  • maxWaitMillis (optional): The amount of time spent waiting for a free connection when all connections in use (defaults to 2000).

Use com.methodhead.persistable.ConnectionSingleton.getConnection(java.lang.String) getConnection() anytime after that to get a connection. In addition, a few convenience methods, com.methodhead.persistable.ConnectionSingleton.runQuery runQuery() , com.methodhead.persistable.ConnectionSingleton.runUpdaterunUpdate() , and com.methodhead.persistable.ConnectionSingleton.runBatchUpdaterunBatchUpdate() are available to run queries using the singleton.

All of these methods accept a name parameter, which enables the creation of named connection pools. This is useful if you want to connect to several databases in the same application. Specify null for name, or use the methods that don't include the name parameter to use the default connection pool.

Important: be sure to close connections obtained from this singleton after you use them as this is how they are returned to the pool; this includes connections associated with result sets returned by runQuery().



Field Summary
final public static  StringDBTYPE_MYSQL
    
final public static  StringDBTYPE_PSQL
    
final public static  StringDBTYPE_SQLSERVER
    
protected static  Mapconnections_
    


Method Summary
public static  voidclose(ResultSet rs)
     A convenience method to close the connection associated with the specified result set.
public static  ConnectiongetConnection(String name)
     Returns a connection from the connection pool named name An exception is thrown if that pool hasn't been intitalized yet or if it is not supplying connections (a likely cause of this is bad connection parameters).
public static  ConnectiongetConnection()
     Returns a connection from the default connection pool.
public static  StringgetDatabaseType(String name)
     Returns one of the DBTYPE_ constants indicating the type of database the name pool is connected to.
public static  StringgetDatabaseType()
     Returns one of the DBTYPE_ constants indicating the type of database the default pool is connected to.
public static  booleaninit(String name, Properties dbProps)
     Initializes the connection pool associated with the name name using the specified properties dbProps, returning true if the singleton was successfully initialized, or false otherwise.
public static  booleaninit(Properties dbProps)
     Initalizes the singleton's default connection pool.
public static  voidrelease(String name)
     Releases the pool with the specified name throwing an exception if no such pool exists.
public static  voidrelease()
     Releases the default pool.
public static  voidrunBatchUpdate(String name, Reader reader)
    

Executes a series of SQL statements read from the specified reader using the connection with the specified name.

public static  voidrunBatchUpdate(Reader reader)
     Executes a series SQL statement using a connection from the default pool.
public static  ResultSetrunQuery(String name, String sql)
     Executes a SQL query using a connection from the pool named name, returning a result set or null if an error occured.
public static  ResultSetrunQuery(String sql)
     Executes a SQL query using a connection from the default pool.
public static  intrunUpdate(String name, String sql)
     Executes a SQL update using a connection from the pool named name.
public static  intrunUpdate(String sql)
     Executes a SQL update using a connection from the default pool.

Field Detail
DBTYPE_MYSQL
final public static String DBTYPE_MYSQL(Code)



DBTYPE_PSQL
final public static String DBTYPE_PSQL(Code)



DBTYPE_SQLSERVER
final public static String DBTYPE_SQLSERVER(Code)



connections_
protected static Map connections_(Code)





Method Detail
close
public static void close(ResultSet rs)(Code)
A convenience method to close the connection associated with the specified result set. Any SQLException thrown while trying to close the connection is logged and ignored. Nothing is done if rs is null.



getConnection
public static Connection getConnection(String name) throws SQLException(Code)
Returns a connection from the connection pool named name An exception is thrown if that pool hasn't been intitalized yet or if it is not supplying connections (a likely cause of this is bad connection parameters). If name is null the default connection pool is assumed. Important: be sure to close the returned connection, as this is how the connection is returned to the pool.



getConnection
public static Connection getConnection() throws SQLException(Code)
Returns a connection from the default connection pool.
See Also:   com.methodhead.persistable.ConnectionSingleton.getConnection(java.lang.String)
See Also:    getConnection()



getDatabaseType
public static String getDatabaseType(String name)(Code)
Returns one of the DBTYPE_ constants indicating the type of database the name pool is connected to. The connection's meta data is examined to determine the database type; if a type cannot be determined, null is returned.



getDatabaseType
public static String getDatabaseType()(Code)
Returns one of the DBTYPE_ constants indicating the type of database the default pool is connected to. The connection's meta data is examined to determine the database type; if a type cannot be determined, null is returned.



init
public static boolean init(String name, Properties dbProps)(Code)
Initializes the connection pool associated with the name name using the specified properties dbProps, returning true if the singleton was successfully initialized, or false otherwise. If a pool already exists for the specified name, false is returned (use com.methodhead.persistable.ConnectionSingleton.release(java.lang.String)release() to release the pool). The following properties are expected to be defined: driver (e.g, org.postgresql.Driver), uri (e.g., jdbc:postgresql:yourdatabase), user, and password. Leading and trailing whitespace is trimmed from property values.



init
public static boolean init(Properties dbProps)(Code)
Initalizes the singleton's default connection pool.
See Also:   com.methodhead.persistable.ConnectionSingleton.init(java.lang.Stringjava.util.Properties)
See Also:    init()



release
public static void release(String name) throws SQLException(Code)
Releases the pool with the specified name throwing an exception if no such pool exists.



release
public static void release() throws SQLException(Code)
Releases the default pool.
See Also:   com.methodhead.persistable.ConnectionSingleton.release(java.lang.String)
See Also:    release()



runBatchUpdate
public static void runBatchUpdate(String name, Reader reader) throws IOException, SQLException(Code)

Executes a series of SQL statements read from the specified reader using the connection with the specified name. The statements are expected in the following format:

  • Statements must be separated by a semicolon.
  • Statements may span several lines, but no statement should begin on the same line another ends.
  • A double dash (--) indicates a comment; any text after the dashes to the end of the line is ignored.
  • SQL keywords should not be mixed case, (e.g., INSERT or insert, but not Insert).
  • SQL statements should not return results (i.e., no SELECT statements, though SELECT INTO is ok).

Note: reader is not closed by this method.




runBatchUpdate
public static void runBatchUpdate(Reader reader) throws IOException, SQLException(Code)
Executes a series SQL statement using a connection from the default pool.
See Also:   com.methodhead.persistable.ConnectionSingleton.runBatchUpdate(java.lang.Stringjava.io.Reader)
See Also:    runBatchUpdate()



runQuery
public static ResultSet runQuery(String name, String sql) throws SQLException(Code)
Executes a SQL query using a connection from the pool named name, returning a result set or null if an error occured. If name is null, the default pool is assumed. Important: be sure to close the connection associated with the returned result set as this is how the connection is returned to the pool (try something like rs.getStatement().getConnection().close() or use ConnectionSingleton.close()).



runQuery
public static ResultSet runQuery(String sql) throws SQLException(Code)
Executes a SQL query using a connection from the default pool.
See Also:   com.methodhead.persistable.ConnectionSingleton.runQuery(java.lang.Stringjava.lang.String)
See Also:    runQuery()



runUpdate
public static int runUpdate(String name, String sql) throws SQLException(Code)
Executes a SQL update using a connection from the pool named name. If name is null the default pool is assumed.



runUpdate
public static int runUpdate(String sql) throws SQLException(Code)
Executes a SQL update using a connection from the default pool. @see com.methodhead.persistable.ConnectionSingleton#runUpdate(java.lang.String,java.lang.String) runUpdate()



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.