Skip to content

Commit

Permalink
Merge pull request #24 from stuken/dev
Browse files Browse the repository at this point in the history
Changes for jndi datasource lookup.
  • Loading branch information
prateepb committed Mar 19, 2015
2 parents 4f460f3 + 97255e0 commit ac72d39
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
2 changes: 2 additions & 0 deletions WebContent/conf/Spiracle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ java.password=test

jdbc.fetchsize=25

default.connection=jndi

spring.path=/path/to/spring-context.xml

application.loggingEnabled=True
Expand Down
12 changes: 10 additions & 2 deletions src/com/waratek/spiracle/init/SpiracleInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -50,6 +52,7 @@ public void contextInitialized(ServletContextEvent arg0) {
logServerInfo(application);
ComboPooledDataSource ds = getConnectionPool(props);
setConnectionPool(application, ds);
setDefaultConnection(application, props);
setFetchSize(application, props);
}

Expand Down Expand Up @@ -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(""));
Expand Down
30 changes: 30 additions & 0 deletions src/com/waratek/spiracle/sql/jndi/CreateJndiConnectionPool.java
Original file line number Diff line number Diff line change
@@ -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.");
}
}
4 changes: 4 additions & 0 deletions src/com/waratek/spiracle/sql/util/ConnectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down
3 changes: 2 additions & 1 deletion src/com/waratek/spiracle/sql/util/SelectUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
}
Expand Down

0 comments on commit ac72d39

Please sign in to comment.