JDBC, Connection Pooling and MySQL
JDBC, Connection Pooling and MySQL
JDBC
Is an API spec. whose implementation comes in the form of jdbc drivers. JDBC API :
java.sql.* javax.sql.*
JDBC DRIVER
Is a bridge s/w between java application and database s/w. Is a java class that implements java.sql.Driver interface. Why we use JDBC Driver?
JDBC ARCHITECTURE
Application
Java
JDBC
Driver
code calls JDBC library JDBC loads a driver Driver talks to a particular database Can have more than one driver -> more than one database
JDBC DRIVERS
Type I: Bridge Type II: Native Type III: Middleware Type IV: Pure
Oracle DB
MS Access
Oracle DB
MS Access
Oracle DB
MS Access
Application Server
JDBC CONNECTION POOL
DB S/W
con
con
con
con
TYPE 3 DRIVER
Is not a driver Its protocol Contains rules required to establish communication between java application and connection pool
ODBC
ODBC Driver
CLI (.lib)
Middleware Server
java.sql
Class.forName(sun.jdbc.odbc.JdbcO dbcDriver);
DRIVERMANAGER
All JDBC Drivers will be registered and managed by DriverManager. When a driver class is first loaded, it registers itself with the DriverManager Therefore, to register a driver, just load it!
CONNECTION
A
Connection represents a session with a specific database. Establishing connection with db s/w is nothing but creating communication channel Can have multiple connections to a database Once task with connection is completed, close the connection.
OBTAINING A CONNECTION
String url = "jdbc:odbc:datasource"; try { Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection(url); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }
STATEMENT
Acts as a courier service to send queries to the db s/w. A Statement object is used for executing a static SQL statement and obtaining the results produced by it.
STATEMENT METHODS
ResultSet executeQuery(String)
int executeUpdate(String)
boolean execute(String)
What
RESULTSET
A
ResultSet object is a java object which can store bunch of selected rows given by select query execution. Only one ResultSet per Statement can be open at once. The table rows are retrieved in sequence. A ResultSet maintains a cursor pointing to its current row of data. The 'next' method moves the cursor to the next row.
RESULTSET
Cursor
BFR
ALR
RESULTSET METHODS
boolean next()
activates the next row the first call to next() activates the first row returns false if there are no more rows
void close()
disposes of the ResultSet allows you to re-use the Statement that created it
RESULTSET METHODS
returns the given field as the given type fields indexed starting at 1 (not 0)
RESULTSET METHODS
String getString(int columnIndex) boolean getBoolean(int columnIndex) byte getByte(int columnIndex) short getShort(int columnIndex) int getInt(int columnIndex) long getLong(int columnIndex) float getFloat(int columnIndex) double getDouble(int columnIndex) Date getDate(int columnIndex) Time getTime(int columnIndex) Timestamp getTimestamp(int columnIndex)
Loads, chooses drivers connects to actual database a series of SQL statements to and from the DB a single SQL statement the records returned from a Statement
Driver
Connection
Statement
ResultSet
Statement
ResultSet
Java App
PREPAREDSTAMENT OBJECT
A query that goes and resides on the DB s/w without values by becoming parsed query is a precompiled query. Precompiled queries will be parsed only once, but capable of executing multiple times with same or different values. PreparedStatement object represents this precompiled query. When to use Statement object and PreparedStatement object ?
Positional parameter indicates value to that query will be set afterwards. Create PreparedStatement object.
PreparedStatement ps=con.prepareStatement(query);
This method makes query as a precompiled query
ps.setInt(-,-), ps.setString(-,-)
ps.close()
CALLABLESTATEMENT
DB s/w can maintain the business logic in the form of PL/SQL procedures and functions. To call PL/SQL procedures and functions from the java app , use CallableStatement. Procedure is a subprogram that performs specific action. A function is a subprogram that computes a value. Functions and procedures are structured alike, except that functions have a RETURN clause
CALLABLESTATEMENT
Y=X*X
out in
X=X*X
inout
cs.setInt(parameter_index,value);
boolean b= cs.execute();
int result=cs.getType(column_index);
For more executions repeat steps 4 , 5, and 6. Close the CallableStatement object.
cs.close();
DATABASE TIME
Java
java.sql.Time
java.sql.Timestamp
Discover database schema information. Discover database users, tables, views, and stored procedures Understand and analyze the result sets returned by SQL queries Determine the signature of a specific stored procedure in the database. Identify the primary/foreign keys for a given table
METADATA IN JDBC
METADATA IN JDBC
If any method invoked in DatabaseMetaData returns 0 or null , it means the driver is not capable of gathering certain information regarding DB s/w Most of the drivers doesnt support ParameterMetaData
JDBC 2.0
Scrollable
Rowsets
JNDI
Connection
SCROLLABLE RESULTSET
ResultSet Object
ResultSet object that allows to access the records in one direction is non-scrollable. ResultSet object that allows to access the records bi-directionally is called scrollable
SCROLLABLE RESULTSET
JDBC 2.0 introduces scrollable results sets whose values can be read and updated if reading and updating is supported by the underlying database. With scrollable result sets, any row can be selected at random, and the result set can be traversed forwards and backwards. One advantage to the new result set is you can update a set of matching rows without having to issue an additional executeUpdate call.
SCROLLABLE RESULTSET
Both Statements and PreparedStatements have an additional constructor that accepts a scroll type and an update type parameter The scroll type value can be one of the following values:
ResultSet.TYPE_FORWARD_ONLY Default behavior in JDBC 1.0, application can only call next() on the result set. ResultSet.SCROLL_SENSITIVE ResultSet is fully navigable and updates are reflected in the result set as they occur. ResultSet.SCROLL_INSENSITIVE Result set is fully navigable, but updates are only visible after the result set is closed. You need to create a new result set to see the results.
SCROLLABLE RESULTSET
The update type parameter can be one of the following two values:
ResultSet.CONCUR_READ_ONLY The result set is read only. ResultSet.CONCUR_UPDATABLE The result set can be updated.
You can verify that your database supports these types by calling :
con.getMetaData().supportsResultSetConcurrency() method
The fully scrollable result set returns a cursor which can be moved using simple commands. By default the result set cursor points to the row before the first row of the result set. A call to next() retrieves the first result set row
beforeFirst(): Default position. Puts cursor before the first row of the result set. first(): Puts cursor on the first row of the result set. last(): Puts cursor before the last row of the result set. afterLast() Puts cursor beyond last row of the result set. Calls to previous moves backwards through the ResultSet. absolute(pos): Puts cursor at the row number position where absolute(1) is the first row and absolute(-1) is the last row. relative(pos): Puts cursor at a row relative to its current position where relative(1) moves row cursor one row forward.
BATCH PROCESSING
Is a process of combining related queries into single unit, sending them to DB s/w as batch and getting their result as a batch. Instead of sending multiple queries to DB s/w for multiple times, send queries as a batch. Reduces network round trips between java application and DB s/w. Helps the programmer to work with Transactions.
BATCH PROCESSING
The calls to stmt.addBatch(-) append statements to the original Statement. The call to executeBatch() submits the entire statement with all the appends to the database. The return result of the addBatch method is an array of row counts affected for each statement executed in the batch job. If a problem occurred, a java.sql.BatchUpdateException is thrown. Once select query is executed , it generates ResultSet object. Since ResultSet object cannot be stored in integer array, batch processing cannot include select queries.
TRANSACTION MANAGEMENT
Is all about combining set of related operations into a single unit and executing them by applying do everything or nothing principle Transactions are not explicitly opened and closed Instead, the connection has a state called AutoCommit mode if AutoCommit is true, then every statement is automatically committed default case: true
SETAUTOCOMMIT Connection.setAutoCommit(boolean) if AutoCommit is false, then every statement is added to an ongoing transaction you must explicitly commit or rollback the transaction using Connection.commit() and Connection.rollback()
ROWSETS
We can send only those java objects over the network which are serializable. ResultSet object is not a serializable object, so we cannot send that object over network. To overcome this problem jdbc has given support for RowSet object which are extension to ResultSet object and can be sent over network. All JDBC drivers doesnt support RowSets.
WHY OPTIMIZE?
On average, a web request performs 4 database queries. Experience has shown that database calls are typical performance bottleneck. Bad JDBC can overwhelm the database.
JDBC API
SQL: SELECT * FROM TABLE java.sql.PreparedStatement java.sql.CallableStatement Cache data on client.
MOST VERSATILE
MOST OPTIMIZATION
JDBC API
SQL STATEMENTS
Most flexible Least reliable Must be recompiled in database for each use
PREPARED STATEMENT
Represents a precompiled SQL statement Can be used to efficiently execute statement multiple times Somewhat flexible can create new ones as needed
JDBC API
CALLABLE STATEMENT
Used to execute SQL stored procedures. Same syntax as PreparedStatement. Least flexible. Most optimized DB call.
PreparedStatement gives better performance when compared to Statement because it is pre-parsed and pre-compiled by the database once for the first time and then onwards it reuses the parsed and compiled statement. CallableStatement gives better performance when compared to PreparedStatement and Statement when there is a requirement for single request to process multiple complex statements
JDBC API
CACHE
Keep data within client to reduce the number of round-trips to the database. Every database schema generally has read-only and readmostly tables. These tables are called as lookup tables. Read-only tables contain static data that never changes in its life time. Read-mostly tables contain semi dynamic data that changes often If an application reads data from these tables for every client request, then it is redundant, unnecessary and expensive. The solution for this problem is to cache the read-only table data by reading the data from that table once and caching the read-mostly table data by reading and refreshing with time limit
Dont useDriverManager.getConnection() often. JDBC connections can take 0.5 to 2 seconds to create. Create Pool of Connections and reuse them. Use multi-threading with Connection Pooling to address network latency Threads can issue queries over separate database connections. Set up proper direction for processing the rows Use proper get methods Close ResultSet when finished
Single-batch Transactions
Collect set of operations and submit transaction in one statement BEGIN TRANSACTION UPDATE TABLE1...
INSERT INTO TABLE2
DELETE TABLE3 COMMIT DB obtains necessary locks on rows and tables, uses and releases them in one step
Smart Queries
Make queries as specific as possible Put more logic into SQL statements DB are designed to use SQL efficiently Proper use of SQL can avoid performance problems
Use database join: SELECT employees.* FROM employees E, dept D WHERE E.DEPTNO = D.DEPTNO AND D.DEPTTYPE = ENG;
Use DB for filtering Use Java for business logic DB does filtering very well DB business logic is poor
CONNECTION POOLING
The JDBC Connection object that is created by the programmer manually is called direct connection object. The JDBC Connection object that is collected from JDBC Connection pool is called pooled connection object. JDBC Connection pool is a factory that contains set of readily available JDBC connection objects.
CONNECTION POOLING
Using connection pools helps to both alleviate connection management overhead and decrease development tasks for data access. Each time an application attempts to access a backend store (such as a database), it requires resources to create, maintain, and release a connection to that datastore. To mitigate the strain this process can place on overall application resources, the Application Server enables administrators to establish a pool of backend connections that applications can share on an application server
Connection pooling can improve the response time of any application that requires connections, especially Web-based applications. With connection pooling, most user requests do not incur the overhead of creating a new connection because the data source can locate and use an existing connection from the pool of connections Re usability, we can reuse the existing connection already existing It also simplifies the design in terms of managing the connections; we can delegate the connection creation, max number of connections for an application, max idle time for a connection etc. using the some kind of a connection pool manager
Classes which adapt traditional DriverManager-based JDBC drivers to the new javax.sql.DataSource scheme for acquiring database Connections. Transparent pooling of Connection and PreparedStatements behind DataSources which can "wrap" around traditional drivers or arbitrary unpooled DataSources.
directly instantiate and configure a ComboPooledDataSource bean use the DataSources factory class "build your own" pool-backed DataSource by directly instantiating PoolBackedDataSource and setting its ConectionPoolDataSource
INSTANTIATING AND CONFIGURING A COMBOPOOLEDDATASOURCE the most straightforward way to create a c3p0 pooling DataSource is to instantiate an instance of com.mchange.v2.c3p0.ComboPooledDataSource This is a JavaBean-style class with a public, no-arg constructor Before you use the DataSource, you'll have to be sure to set at least the property jdbcUrl You may also want to set user and password, and if you have not externally preloaded the old-style JDBC driver you'll use you should set the driverClass.
Community Editions free Enterprise Editions free license but a subscription and maintenance fee
Suitable situations range from small enterprise to global, enterprise-wide systems Multiple platforms
MYSQL TOOLS
mysqladmin
SQLyog
is a GUI front-end for MySQL (there are others) - Community and Enterprise Editions Allows Data Definition and Data Manipulation After loading it and before using it you need to Connect to the right MySQL instance A tutorial, with a range of activities to get familiar with it, is available at http://www.databasejournal.com/features/mysql /article.php/1558731
SQLYOG SCREEN
REFERENCES