[jboss-cvs] JBossAS SVN: r110545 - projects/jboss-jca/branches/performance/jmeter/src/main/java/org/jboss/jca/performance/servlet/http.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Feb 4 16:13:50 EST 2011
Author: whitingjr
Date: 2011-02-04 16:13:50 -0500 (Fri, 04 Feb 2011)
New Revision: 110545
Modified:
projects/jboss-jca/branches/performance/jmeter/src/main/java/org/jboss/jca/performance/servlet/http/HttpConnectionServlet.java
projects/jboss-jca/branches/performance/jmeter/src/main/java/org/jboss/jca/performance/servlet/http/HttpSelectServlet.java
Log:
Updated servlet classes to cache the DataSource. Updated Select servlet to break out the work into methods.
Modified: projects/jboss-jca/branches/performance/jmeter/src/main/java/org/jboss/jca/performance/servlet/http/HttpConnectionServlet.java
===================================================================
--- projects/jboss-jca/branches/performance/jmeter/src/main/java/org/jboss/jca/performance/servlet/http/HttpConnectionServlet.java 2011-02-04 17:18:56 UTC (rev 110544)
+++ projects/jboss-jca/branches/performance/jmeter/src/main/java/org/jboss/jca/performance/servlet/http/HttpConnectionServlet.java 2011-02-04 21:13:50 UTC (rev 110545)
@@ -56,13 +56,13 @@
public static final String SELECT_STATEMENT = "SELECT firstname FROM Customer WHERE id=0;";
public static final String UPDATE_STATEMENT = "UPDATE Customer SET firstname=? WHERE id=0;";
private Context context = null;
+ private DataSource dataSource = null;
+ private UserTransaction userTransaction = null;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
- UserTransaction userTransaction = null;
- DataSource dataSource = null;
Connection connection = null;
PreparedStatement selectPreparedStatement= null;
PreparedStatement updatePreparedStatement = null;
@@ -72,9 +72,8 @@
{
if (null != this.context)
{
- userTransaction = (UserTransaction)context.lookup(JNDI_USER_TRANSACTION);
+
userTransaction.begin();
- dataSource = (DataSource)this.context.lookup(JNDI_NAME);
connection = dataSource.getConnection();
selectPreparedStatement = connection.prepareStatement(SELECT_STATEMENT);
resultSet = selectPreparedStatement.executeQuery();
@@ -150,6 +149,8 @@
try
{
this.context = new InitialContext();
+ this.userTransaction = (UserTransaction)context.lookup(JNDI_USER_TRANSACTION);
+ this.dataSource = (DataSource)this.context.lookup(JNDI_NAME);
}
catch (Exception e)
{
@@ -171,7 +172,10 @@
if (null != this.context)
{
this.context.close();
+ this.context = null;
}
+ this.dataSource = null;
+ this.userTransaction = null;
}
catch (Exception e)
{
Modified: projects/jboss-jca/branches/performance/jmeter/src/main/java/org/jboss/jca/performance/servlet/http/HttpSelectServlet.java
===================================================================
--- projects/jboss-jca/branches/performance/jmeter/src/main/java/org/jboss/jca/performance/servlet/http/HttpSelectServlet.java 2011-02-04 17:18:56 UTC (rev 110544)
+++ projects/jboss-jca/branches/performance/jmeter/src/main/java/org/jboss/jca/performance/servlet/http/HttpSelectServlet.java 2011-02-04 21:13:50 UTC (rev 110545)
@@ -4,9 +4,11 @@
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
+import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
+import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
@@ -14,6 +16,11 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.NotSupportedException;
+import javax.transaction.RollbackException;
+import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
public class HttpSelectServlet extends HttpServlet
@@ -29,30 +36,26 @@
public static final String SUCCESS = "SUCCESS";
public static final String SELECT_STATEMENT = "SELECT 1;";
private Context context = null;
+ private DataSource dataSource = null;
+ private UserTransaction userTransaction = null;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
- UserTransaction userTransaction = null;
- DataSource dataSource = null;
Connection connection = null;
PreparedStatement selectPreparedStatement = null;
- ResultSet resultSet = null;
String result = FAIL;
try
{
- if (null != this.context)
+ if (null != this.context && null != this.dataSource && null != this.userTransaction)
{
- userTransaction = (UserTransaction) context
- .lookup(JNDI_USER_TRANSACTION);
- userTransaction.begin();
- dataSource = (DataSource) this.context.lookup(JNDI_NAME);
- connection = dataSource.getConnection();
- selectPreparedStatement = connection
- .prepareStatement(SELECT_STATEMENT);
- resultSet = selectPreparedStatement.executeQuery();
- if (resultSet.next())
+ userTransaction = lookup();
+ begin(userTransaction);
+ dataSource = lookupDS();
+ connection = getConnection(dataSource);
+ selectPreparedStatement = getPS(connection);
+ if (find(selectPreparedStatement))
{
result = SUCCESS;
}
@@ -62,24 +65,24 @@
getServletContext().log(e.getMessage(), e);
} finally
{
+// try
+// {
+// if (null != resultSet)
+// resultSet.close(); a resultset is closed when the statement is closed
+// } catch (Exception e)
+// {
+// }
try
{
- if (null != resultSet)
- resultSet.close();
- } catch (Exception e)
- {
- }
- try
- {
if (null != selectPreparedStatement)
- selectPreparedStatement.close();
+ psClose(selectPreparedStatement);
} catch (Exception e)
{
}
try
{
if (null != connection)
- connection.close();
+ close(connection);
} catch (Exception e)
{
}
@@ -89,10 +92,10 @@
{
if (SUCCESS == result)
{
- userTransaction.commit();
+ commit();
} else
{
- userTransaction.rollback();
+ rollback();
}
}
} catch (Exception e)
@@ -119,6 +122,8 @@
try
{
this.context = new InitialContext();
+ this.userTransaction = (UserTransaction)context.lookup(JNDI_USER_TRANSACTION);
+ this.dataSource = (DataSource)this.context.lookup(JNDI_NAME);
} catch (Exception e)
{
log(e.getMessage());
@@ -141,9 +146,67 @@
{
this.context.close();
}
- } catch (Exception e)
+ this.dataSource = null;
+ this.userTransaction = null;
+ }
+ catch (Exception e)
{
log(e.getMessage());
}
}
+ private UserTransaction lookup()
+ throws NamingException
+ {
+ return (UserTransaction) context
+ .lookup(JNDI_USER_TRANSACTION);
+ }
+
+ private void begin(UserTransaction ut)
+ throws SystemException, NotSupportedException
+ {
+ ut.begin();
+ }
+ private DataSource lookupDS()
+ throws NamingException
+ {
+ return (DataSource) this.context.lookup(JNDI_NAME);
+ }
+ private Connection getConnection (DataSource ds)
+ throws SQLException
+ {
+ return ds.getConnection();
+ }
+ private boolean find( PreparedStatement selectPreparedStatement)
+ throws SQLException
+ {
+ ResultSet resultSet = selectPreparedStatement.executeQuery();
+ return resultSet.next();
+ }
+ private PreparedStatement getPS(Connection connection)
+ throws SQLException
+ {
+ return connection.prepareStatement(SELECT_STATEMENT);
+ }
+ private void commit()
+ throws SystemException, HeuristicRollbackException, HeuristicMixedException, SecurityException, IllegalStateException, RollbackException
+ {
+ this.userTransaction.commit();
+ }
+ private void rollback()
+ throws SystemException, SecurityException, IllegalStateException
+ {
+ this.userTransaction.rollback();
+ }
+
+ private void close(Connection conn)
+ throws SQLException
+ {
+ conn.close();
+ }
+ private void psClose(PreparedStatement ps)
+ throws SQLException
+ {
+ ps.close();
+ }
+
}
More information about the jboss-cvs-commits
mailing list