先是关于连接池,我仅用了struts框架,采用dbcp连接池server.xml如下:
<Context path="" docBase="ROOT" reloadable="true" crossContext="true">
<Resource
name="jdbc/test"
auth="Container"
type="javax.sql.DataSource"
username="root"
password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/test"
maxActive="200"
minIdle="10"
maxIdle="30"
maxWait="10000"
removeAbandoned="true"
removeAbandonedTimeout="180"
/>
</Context>ConnFactory :package com.kuyi.util;import java.sql.Connection;
//import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;import org.apache.commons.dbcp.BasicDataSource;public class ConnFactory {
private static DataSource source = null; public static Connection getConnection() {
Connection conn = null;
if (source == null) {
// source = getDataSource("jdbc/kuyi");
// 取得数据库连接
// 加载驱动
try {
conn = ConnFactory.getDataSource().getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("conn:" + conn);
return conn;
}
public Connection getConn(String dsnName)
{
Connection conn = null;
Context ctx = null;
DataSource ds = null;
String jndiStr = null;
try{
jndiStr = "java:comp/env/jdbc/" + dsnName.trim();
System.out.println("jndiStr=[" + jndiStr + "]");
ctx = new InitialContext();
ds = (DataSource) ctx.lookup(jndiStr);
conn = ds.getConnection();
}
catch(Exception e) {
System.out.println("Connect database [" + dsnName.trim() + "] Error:" + e);
}
return conn;
}
/**
* 关闭连接
*
* @param conn
*/
public static void close(Connection conn) { try {
if (conn != null && !conn.isClosed()) {
conn.close();
conn = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
conn = null;
} } private static DataSource getDataSource(String ctxName) {
DataSource ds = null;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
ds = (DataSource) envCtx.lookup(ctxName);
} catch (Exception e) {
e.printStackTrace();
} return ds; } public static void close(ResultSet rs, Statement st) {
try {
if (rs != null && rs.next()) {
rs.close();
rs = null;
}
if (st != null) {
st.close();
st = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
rs = null;
st = null;
}
} public static void close(ResultSet rs, PreparedStatement pst) {
try {
if (rs != null && rs.next()) {
rs.close();
rs = null;
}
if (pst != null) {
pst.close();
pst = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
rs = null;
pst = null;
}
} public static void close(ResultSet rs, PreparedStatement pst,
Connection conn) {
try {
if (rs != null && rs.next()) {
rs.close();
rs = null;
}
if (pst != null) {
pst.close();
pst = null;
} if (conn != null && !conn.isClosed()) {
conn.close();
conn = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
rs = null;
pst = null;
conn = null;
}
} public static void close(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null && rs.next()) {
rs.close();
rs = null;
}
if (st != null) {
st.close();
st = null;
} if (conn != null && !conn.isClosed()) {
conn.close();
conn = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
rs = null;
st = null;
conn = null;
}
} public static void close(PreparedStatement pst, Connection conn) {
try { if (pst != null) {
pst.close();
pst = null;
} if (conn != null && !conn.isClosed()) {
conn.close();
conn = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
pst = null;
conn = null;
}
} public static void close(PreparedStatement pst) {
if (pst != null) {
try {
pst.close();
pst = null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
pst = null;
}
}
} public static void close(Statement st) {
if (st != null) {
try {
st.close();
st = null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
st = null;
}
}
} private static BasicDataSource ds = null; public static BasicDataSource getDataSource() {
if (ds == null) {
ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://192.168.1.2:3306/test2");
ds.setUsername("root");
ds.setPassword("root");
ds.setMaxActive(50);
}
return ds;
}
}webroot-web.xml <resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
app:
public void updatePaymentMendState(int id, String state, String operator) {
new PaymentMendDAo().updateState(conn, id, state, operator);
System.out.println(conn);
ConnFactory.close(conn);
}
请问下我这样配置连接池还有什么问题没有?
另外我有一次在循环里System.out.println("1");的时候,服务器在循环打印过程(10秒)中一直卡住了,那在服务器上打印是不是应该都去掉来减少服务器资源消耗?
最后一个问题是tomcat服务器在运行时候到底是开有打印显示黑屏的那个tomcat[/tomcat/bin/tomcat6.exe]还是开不带显示的那个[/tomcat/bin/tomcat6w.exe],他们区别在哪里?没有部署经验,还请高手们包涵.
<Context path="" docBase="ROOT" reloadable="true" crossContext="true">
<Resource
name="jdbc/test"
auth="Container"
type="javax.sql.DataSource"
username="root"
password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/test"
maxActive="200"
minIdle="10"
maxIdle="30"
maxWait="10000"
removeAbandoned="true"
removeAbandonedTimeout="180"
/>
</Context>ConnFactory :package com.kuyi.util;import java.sql.Connection;
//import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;import org.apache.commons.dbcp.BasicDataSource;public class ConnFactory {
private static DataSource source = null; public static Connection getConnection() {
Connection conn = null;
if (source == null) {
// source = getDataSource("jdbc/kuyi");
// 取得数据库连接
// 加载驱动
try {
conn = ConnFactory.getDataSource().getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("conn:" + conn);
return conn;
}
public Connection getConn(String dsnName)
{
Connection conn = null;
Context ctx = null;
DataSource ds = null;
String jndiStr = null;
try{
jndiStr = "java:comp/env/jdbc/" + dsnName.trim();
System.out.println("jndiStr=[" + jndiStr + "]");
ctx = new InitialContext();
ds = (DataSource) ctx.lookup(jndiStr);
conn = ds.getConnection();
}
catch(Exception e) {
System.out.println("Connect database [" + dsnName.trim() + "] Error:" + e);
}
return conn;
}
/**
* 关闭连接
*
* @param conn
*/
public static void close(Connection conn) { try {
if (conn != null && !conn.isClosed()) {
conn.close();
conn = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
conn = null;
} } private static DataSource getDataSource(String ctxName) {
DataSource ds = null;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
ds = (DataSource) envCtx.lookup(ctxName);
} catch (Exception e) {
e.printStackTrace();
} return ds; } public static void close(ResultSet rs, Statement st) {
try {
if (rs != null && rs.next()) {
rs.close();
rs = null;
}
if (st != null) {
st.close();
st = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
rs = null;
st = null;
}
} public static void close(ResultSet rs, PreparedStatement pst) {
try {
if (rs != null && rs.next()) {
rs.close();
rs = null;
}
if (pst != null) {
pst.close();
pst = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
rs = null;
pst = null;
}
} public static void close(ResultSet rs, PreparedStatement pst,
Connection conn) {
try {
if (rs != null && rs.next()) {
rs.close();
rs = null;
}
if (pst != null) {
pst.close();
pst = null;
} if (conn != null && !conn.isClosed()) {
conn.close();
conn = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
rs = null;
pst = null;
conn = null;
}
} public static void close(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null && rs.next()) {
rs.close();
rs = null;
}
if (st != null) {
st.close();
st = null;
} if (conn != null && !conn.isClosed()) {
conn.close();
conn = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
rs = null;
st = null;
conn = null;
}
} public static void close(PreparedStatement pst, Connection conn) {
try { if (pst != null) {
pst.close();
pst = null;
} if (conn != null && !conn.isClosed()) {
conn.close();
conn = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
pst = null;
conn = null;
}
} public static void close(PreparedStatement pst) {
if (pst != null) {
try {
pst.close();
pst = null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
pst = null;
}
}
} public static void close(Statement st) {
if (st != null) {
try {
st.close();
st = null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
st = null;
}
}
} private static BasicDataSource ds = null; public static BasicDataSource getDataSource() {
if (ds == null) {
ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://192.168.1.2:3306/test2");
ds.setUsername("root");
ds.setPassword("root");
ds.setMaxActive(50);
}
return ds;
}
}webroot-web.xml <resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
app:
public void updatePaymentMendState(int id, String state, String operator) {
new PaymentMendDAo().updateState(conn, id, state, operator);
System.out.println(conn);
ConnFactory.close(conn);
}
请问下我这样配置连接池还有什么问题没有?
另外我有一次在循环里System.out.println("1");的时候,服务器在循环打印过程(10秒)中一直卡住了,那在服务器上打印是不是应该都去掉来减少服务器资源消耗?
最后一个问题是tomcat服务器在运行时候到底是开有打印显示黑屏的那个tomcat[/tomcat/bin/tomcat6.exe]还是开不带显示的那个[/tomcat/bin/tomcat6w.exe],他们区别在哪里?没有部署经验,还请高手们包涵.
解决方案 »
- dom4j解析xml
- hashcode和equals
- 【求代码】那位大侠能给一段Linux下 java Scoket的小程序啊 !!
- 请问分布式与cluster有什么区别,大并发量选择哪个好
- 如何在NetBeans中使用Spring框架?
- 一个关于随要码重复率的问题.
- JAVA菜鸟救助.
- 现想java,不知道从那入手,各位师兄指点一下。。。。。。。
- A question about access Active Directory information via JNDI?
- 求助 quartz 设置时间段
- java中如何实现远程调用http来获取返回来的数据
- 有关Hibernate删除的简单的问题
2、启tomcat应该启bin\startup.bat,你说的两个都不是
现在还是过一段时间后,就报错:连接已满200
关的时候最好三个都关掉/**
* 关闭连接。
* @param conn
* @param stmt
* @param rs
*/
public static void close (ResultSet rs, PreparedStatement stmt, Connection conn) {
try {
if(rs !=null)
rs.close ();
} catch (Exception e) {
Debug.printErr (e.getMessage ());
}
try {
if(stmt !=null)
stmt.close ();
} catch (Exception e) {
Debug.printErr (e.getMessage ());
}
try {
if(conn !=null)
conn.close ();
} catch (Exception e) {
Debug.printErr (e.getMessage ());
}
}
明显是你的close不起作用。按7楼的方式修改下close。
DBCon.close(con, stm, rs);
}public static void close(Connection con,Statement stm,ResultSet rs){
if(con!=null){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stm!=null){
try {
stm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}