diff --git a/WebContent/conf/Spiracle.properties b/WebContent/conf/Spiracle.properties index b4e00bf..8728dfa 100644 --- a/WebContent/conf/Spiracle.properties +++ b/WebContent/conf/Spiracle.properties @@ -11,6 +11,8 @@ java.password=test jdbc.fetchsize=25 +default.connection=jndi + spring.path=/path/to/spring-context.xml application.loggingEnabled=True diff --git a/src/com/waratek/spiracle/init/SpiracleInit.java b/src/com/waratek/spiracle/init/SpiracleInit.java index 008088f..437f4af 100644 --- a/src/com/waratek/spiracle/init/SpiracleInit.java +++ b/src/com/waratek/spiracle/init/SpiracleInit.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.Properties; + import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; @@ -38,8 +39,9 @@ public class SpiracleInit implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent arg0) { - // TODO Auto-generated method stub - + ServletContext application = arg0.getServletContext(); + ComboPooledDataSource ds = (ComboPooledDataSource) application.getAttribute("connectionPool"); + ds.close(); } @Override @@ -50,6 +52,7 @@ public void contextInitialized(ServletContextEvent arg0) { logServerInfo(application); ComboPooledDataSource ds = getConnectionPool(props); setConnectionPool(application, ds); + setDefaultConnection(application, props); setFetchSize(application, props); } @@ -126,6 +129,11 @@ private void setFetchSize(ServletContext application, Properties props) { } } + private void setDefaultConnection(ServletContext application, Properties props) { + String defaultConnection = (String) props.get("default.connection"); + application.setAttribute("defaultConnection", defaultConnection); + } + void logServerInfo(ServletContext application) { logger.info("Application Server Name: " + application.getServerInfo()); logger.info("Application Context Path:" + application.getRealPath("")); diff --git a/src/com/waratek/spiracle/sql/jndi/CreateJndiConnectionPool.java b/src/com/waratek/spiracle/sql/jndi/CreateJndiConnectionPool.java new file mode 100644 index 0000000..1e50c53 --- /dev/null +++ b/src/com/waratek/spiracle/sql/jndi/CreateJndiConnectionPool.java @@ -0,0 +1,30 @@ +package com.waratek.spiracle.sql.jndi; + +import javax.annotation.Resource; +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; +import javax.servlet.annotation.WebListener; +import javax.sql.DataSource; + +import org.apache.log4j.Logger; + +@WebListener +public class CreateJndiConnectionPool implements ServletContextListener { + private static final Logger logger = Logger.getLogger(CreateJndiConnectionPool.class); + + @Resource(name = "jdbc/oracle") + DataSource ds; + + @Override + public void contextDestroyed(ServletContextEvent arg0) { + + } + + @Override + public void contextInitialized(ServletContextEvent arg0) { + ServletContext application = arg0.getServletContext(); + application.setAttribute("jndiConnectionPool", ds); + logger.info("Added jndi connection pool " + ds + " to application context."); + } +} diff --git a/src/com/waratek/spiracle/sql/util/ConnectionUtil.java b/src/com/waratek/spiracle/sql/util/ConnectionUtil.java index 94081a2..15230dd 100644 --- a/src/com/waratek/spiracle/sql/util/ConnectionUtil.java +++ b/src/com/waratek/spiracle/sql/util/ConnectionUtil.java @@ -19,6 +19,7 @@ import java.sql.SQLException; import javax.servlet.ServletContext; +import javax.sql.DataSource; import org.apache.log4j.Logger; import org.springframework.context.support.FileSystemXmlApplicationContext; @@ -42,6 +43,9 @@ public static Connection getConnection(ServletContext application, String connec FileSystemXmlApplicationContext context = (FileSystemXmlApplicationContext)application.getAttribute("springContext"); DriverManagerDataSource dmds = (DriverManagerDataSource)context.getBean("dataSource"); con = dmds.getConnection(); + } else if(connectionType.equals("jndi")) { + DataSource ds = (DataSource) application.getAttribute("jndiConnectionPool"); + con = ds.getConnection(); } logger.info("Returning connection: " + con.toString()); diff --git a/src/com/waratek/spiracle/sql/util/SelectUtil.java b/src/com/waratek/spiracle/sql/util/SelectUtil.java index 9eb0f58..a902758 100644 --- a/src/com/waratek/spiracle/sql/util/SelectUtil.java +++ b/src/com/waratek/spiracle/sql/util/SelectUtil.java @@ -39,6 +39,7 @@ public static void executeQuery(String sql, ServletContext application, HttpServ String connectionType = null; Connection con = null; int fetchSize = (Integer) application.getAttribute("fetchSize"); + String defaultConnection = (String) application.getAttribute("defaultConnection"); PreparedStatement stmt = null; ResultSet rs = null; @@ -50,7 +51,7 @@ public static void executeQuery(String sql, ServletContext application, HttpServ try { //Checking if connectionType is set, defaulting it to c3p0 if not set. if(request.getParameter("connectionType") == null) { - connectionType = "c3p0"; + connectionType = defaultConnection; } else { connectionType = request.getParameter("connectionType"); }