Server-Side Web Programming: Efficient and Safe Database Access On Web Servers
Server-Side Web Programming: Efficient and Safe Database Access On Web Servers
Web Programming
Lecture 14:
Efficient and Safe Database
Access on Web Servers
Synchronized Database Access
• Many database updates can occur “simultaneously” on busy sites
• Can interfere with one another
• Example: Quantity update after purchase
– Query for previous quantity
– Subtract 1
– Update database with new quantity
Synchronized Database Access
• Java runs separate clients as “parallel” threads which execute
“simultaneously”
– Processor swaps back and forth between threads
• Problem if following sequence occurs:
– Current quantity = 100
– Client 1 code to get current quantity executes (value = 100)
– Processor swaps to client 2 thread
– Client 2 code to get current quantity (value still = 100)
– Client 2 code sets new quantity to 99 and stores in database
– Processor swaps back to client 1 thread
– Client 1 code also sets new quantity to 99 and stores in database!
Synchronized Database Access
Client 2
thread Get quantity Set quantity = 99
Quantity = 100 Store 99 in
database
Synchronized Database Access
• Can declare sections of code to be synchronized
– Only one thread may execute it at a time
– Another thread cannot start the code until the first has finished it
• Syntax: synchronized(object) { code }
Only one thread at a time should be able to execute this code on this object
Synchronized Database Access
Efficiency in Database Access
• Database access most time consuming part of most e-commerce
transactions
• Most costly parts:
– Creating new connections to database
– Creating new statements using those connections
• Idea:
Do as much as possible in advance
– Prepared statements
– Connection pooling
Prepared Statements
• Executing a statement takes time for database server
– Parses SQL statement and looks for syntax errors
– Determines optimal way to execute statement
• Particularly for statements involving loading multiple tables
• Syntax:
– Define PreparedStatement object instead of Statement
productCode = request.getParameter("productCode");
title = request.getParameter("title");
price = Double.parseDouble(request.getParameter("price"));
web container
Connection pool database server
database
Connection Pooling
• When connection requested:
– Get unused connection from pool
Connections currently
web container in use
JSP/servlet
Connection pool database server
JSP/servlet
database
Request for new
connection
New
JSP/servlet
Free unused
connection
Connection Pooling
• When connection requested:
– Connection used by servlet/JSP
Connections currently
web container in use
JSP/servlet
Connection pool database server
JSP/servlet
database
Connections currently
web container in use
JSP/servlet
Connection pool database server
JSP/servlet
database
Return to pool
New
JSP/servlet
Free unused
connection
Connection Pooling
• Unlike prepared statement, no built in Java methods/classes
– Write your own
• http://java.sun.com/developer/onlineTraining/Programming
/JDCBook/conpool.html
– Third party classes
• dbConnectionBroker, etc.
– Build components directly into web.xml/context.xml files
• Page 466 of text
• Not well supported by Tomcat
Connection Pooling
• Usually static object
– Automatically constructs connections first time getConnection called
• Usually provide following methods:
– ConnectionPool.getInstance()
– freeConnection()
• Example:
connection.freeConnection();
Connection Pooling
• Required parameters:
– Driver name Necessary so
• "com.mysql.jdbc.Driver“ connection pool can
– Url, name, and password connect to database
• "jdbc:mysql://localhost/bookstore",
“root", “sesame"
– Number of initial connections to create
• Usually a few hundred to a few thousand
– Timeout for idle connections
• Time after which idle connections are returned to pool automatically
• Important to prevent pool running out!