04a - Improvements on database access and Javabeans

04a - Improvements on database access and Javabeans - CSE...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CSE 135 Improvements on how to access the database: Connection Pools Handling Database Connections Within a JSP: Opening a connection for every HTTP request penalizes the DB server (in terms of resources) and the client (in terms of waiting time) Hardcoded JSBC driver, database name, username and password reduce portability Need to repeat for every JSP accessing the DB – Code maintenance becomes almost impossible 2 Mix HTML presentation code and DB access code – Bad system design
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 (Poor) Alternative: Handling Database Connections Within Java Servlet init() method: Close connection in destroy() Connection open for the lifetime of the servlet Portability, code maintenance and HTML/DB code mixing arguments apply here too Bad system design 3 Better Alternative: Connection Pool Application server creates a resource that is a pool of connections to a DBMS So that each web app process does not have to open and close a connection Developer specifies: Pool size Minimum number of open connections 4 – Even if nobody asked them yet Minimum number of connections that will not close Timeouts
Background image of page 2
3 Browser HTTP The Connection Pool as a Proxy to the database App Server HTML Requests JSPs Connection Pool 5 Database Server Tuples JDBC Requests In META-INF/context.xml <?xml version="1.0" encoding="UTF-8"?> <Context path="" debug="5" override="true" reloadable="true"> <Resource name="jdbc/ClassesDBPool" description="CSE Classes DB Pool" driverClassName="com.mysql.jdbc.Driver" type="javax.sql.DataSource” auth="Container" url="jdbc:mysql://localhost/DemoClasses” username="root” password="root" defaultAutoCommit="false” Name Connection Info JDBC Driver 6 maxActive="10” minIdle="0” maxIdle="5” maxWait="3000" removeAbandoned="true” removeAbandonedTimeout=”60" logAbandoned="true” validationQuery="SELECT 1” /> </Context> Pool Info
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Data Entry Form - 4 th Attempt JSP Code <html><body><table><tr> <td><jsp:include page="menu.html”/> </td> <td> <Open Connection Code> <Insertion Code> <Update Code> <Delete Code> <Statement Code> 7 <Presentation Code> <Close Connection Code> </td> </tr></table></body></html> Data Entry Form - 4 th Attempt <%-- Import packages --%> <%@ page import="java.sql.*, javax.sql.* , javax.naming.* "%> <%-- Open Connection Code --%> <% Connection conn = null; try { // Obtain the environment naming context Context initCtx = new InitialContext(); // Look up the data source 8 DataSource ds = (DataSource) initCtx.lookup("java:comp/env/jdbc/ClassesDBPool"); // Allocate and use a connection from the pool conn = ds.getConnection(); %>
Background image of page 4
5 DB Specific Parameters Databases close connections after many hours of inactivity – fix this <?xml version="1.0" encoding="UTF-8"?> <Context path="" debug="5" override="true" reloadable="true"> <Resource ...
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/31/2011 for the course CSE 135 taught by Professor Papakonstantinou during the Fall '10 term at UCSD.

Page1 / 17

04a - Improvements on database access and Javabeans - CSE...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online