因为开发的时候系统是用proxool来实现数据库连接的,而生产环境是用was的所以采用jndi的方式连接。
为了每次上应用的时候不用去改配置数据库配置,所以把原因proxool改成了jndi的方式。
proxool的实现
web.xml:
<context-param>
<param-name>xmlFile</param-name>
<param-value>WEB-INF/conf/proxool/proxool.xml</param-value>
</context-param>
<listener>
<listener-class>
com.test.listener.ProxoolListener
</listener-class>
</listener>
applicationContext.xml:
<bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
</property>
<property name="url">
<value>proxool.datasource1</value>
</property>
</bean>
proxool.xml:
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>datasource1</alias>
<driver-url>jdbc:oracle:thin:@192.168.2.137:1521:orcl</driver-url>
<driver-class>oracle.jdbc.OracleDriver</driver-class>
<driver-properties>
<property name="user" value="test" />
<property name="password" value="123456" />
</driver-properties>
<maximum-connection-count>5</maximum-connection-count>
<minimum-connection-count>1</minimum-connection-count>
<prototype-count>2</prototype-count>
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<house-keeping-test-sql>select SYSDATE FROM DUAL</house-keeping-test-sql>
<statistics>1m,1h,1d</statistics>
<statistics-log-level>ERROR</statistics-log-level>
</proxool>
</something-else-entirely>
ProxoolListener类
public class ProxoolListener
implements ServletContextListener
{
private static final Logger LOG = LoggerFactory.getLogger(ProxoolListener.class);
private static final String XML_FILE_PROPERTY = "xmlFile";
private static final String PROPERTY_FILE_PROPERTY = "propertyFile";
private static final String AUTO_SHUTDOWN_PROPERTY = "autoShutdown";
private boolean autoShutdown;
public ProxoolListener()
{
this.autoShutdown = true; }
public void contextDestroyed(ServletContextEvent arg0) {
if (this.autoShutdown)
ProxoolFacade.shutdown(0);
}
public void contextInitialized(ServletContextEvent contextEvent)
{
ServletContext context = contextEvent.getServletContext();
String appDir = contextEvent.getServletContext().getRealPath("/");
Properties properties = new Properties();
Enumeration names = context.getInitParameterNames();
while (names.hasMoreElements()) {
String name = (String)names.nextElement();
String value = context.getInitParameter(name);
if (name.equals("xmlFile"))
try {
File file = new File(value);
if (file.isAbsolute())
JAXPConfigurator.configure(value, false);
else
JAXPConfigurator.configure(appDir + File.separator + value, false);
}
catch (ProxoolException e)
{
LOG.error("Problem configuring " + value, e);
}
else if (name.equals("propertyFile"))
try {
File file = new File(value);
if (file.isAbsolute())
PropertyConfigurator.configure(value);
else
PropertyConfigurator.configure(appDir + File.separator + value);
}
catch (ProxoolException e)
{
LOG.error("Problem configuring " + value, e);
}
else if (name.equals("autoShutdown"))
this.autoShutdown = Boolean.valueOf(value).booleanValue();
else if (name.startsWith("jdbc")) {
properties.setProperty(name, value);
}
}
if (properties.size() <= 0) return;
try {
PropertyConfigurator.configure(properties);
} catch (ProxoolException e) {
LOG.error("Problem configuring using init properties", e);
}
}
}
jndi配置方式
web.xml:
<resource-ref>
<res-ref-name>jdbc/test_xa</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
applicationContext.xml:
<jee:jndi-lookup id="dataSource1"
jndi-name="jdbc/test_xa"
cache="true"
resource-ref="true"
lookup-on-startup="false"
proxy-interface="javax.sql.DataSource"/>
找到tomcat的conf目录下的context.xml在Contex之间加代码:
<Resource name="jdbc/test_xa"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@192.168.2.137:1521:orcl"
username="test"
password="123456"
maxActive="100"
maxIdle="30"
maxWait="10000" />