我现在用连接池连接数据库,用tomcat做本地服务器一切正常,但是上传正式服务器之后,隔几天不用,项目就不能进行数据库操作了。重启一下服务器或者重新上传项目文件到服务器又可以正常使用了,不知道这是什么原因!
解决方案 »
- 在JDK1.5下tomcat服务器上运行XFIRE客户端
- hibernate中外键关联的对象的属性在struts中显示的问题,总是要先在控制台System.out.println(xxx)后才可以显示,请指教,谢谢
- jsp字符串的处理问题 谢谢
- 想用jsp做一个网站的自动问答系统
- Struts页面控制Bean属性
- 一个关于Canvas ca = new canvas(curimg)的问题,欢迎大家指正
- Delphi 调取java写的webservice 问题
- 与SESSION有关的问题??
- 50分 求救——resin-JSP的GLOBAL.JSA 如何配置
- 大文件上传有什么好的方法
- 各位:jsp页面中怎样读取文件
- 关于HibernateTemplate下的update
private static Properties prop = new Properties();
/** 数据源 */
private static DataSource ds = null;
//用来把Connection绑定到当前线程上的变量
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); static{
try {
prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbc.properties"));
} catch (IOException e) {
e.printStackTrace();
System.out.println("在classpath下没有找到jdbc.properties文件");
}
//使用C3P0连接池技术
try {
Class.forName("com.mysql.jdbc.Driver");
DataSource unpooled = DataSources.unpooledDataSource(
prop.getProperty("url"),
prop.getProperty("user"),
prop.getProperty("password"));
ds = DataSources.pooledDataSource(unpooled);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 根据数据库的默认连接参数获取数据库的Connection对象
* @return 成功,返回Connection对象,否则返回null
*/
public static synchronized Connection getConnection(){
Connection conn = tl.get(); //先从当前线程上取出连接实例
if(null == conn){ //如果当前线程上没有Connection的实例
try {
conn = ds.getConnection(); // 从连接池中取出一个连接实例
tl.set(conn); //把它绑定到当前线程上
} catch (SQLException e) {
e.printStackTrace();
}
}
return conn;
}
/**
* 获取事务管理器
* @return
*/
public static synchronized TransactionManager getTranManager(){
return new TransactionManager(getConnection());
}
protected static void close(Connection conn) throws DaoException{
if(conn != null){
try {
conn.close();
tl.remove(); //卸载线程绑定
} catch (SQLException e) {
throw new DaoException("关闭连接时出现异常",e);
}
}
}
}
private static DataSource ds=null;
/** 线程局部化的Connection */
private static ThreadLocal<Connection> t1=new ThreadLocal<Connection>();
static{
try{
properties.load(Thread.currentThread().getContextClassLoader().
getResourceAsStream("jdbc.properties"));
Class.forName(properties.getProperty("driver"));
ds=DataSources.unpooledDataSource(properties.getProperty("url"),properties.getProperty("user"),
properties.getProperty("password"));
ds=DataSources.pooledDataSource(ds);
}catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("加载数据库驱动类失败!!!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("在classpath下没有找到jdbc.properties文件");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("创建连接池失败!!!");
}
}
private DbUtils() {
}
/**
* 根据数据库的默认连接参数获取线程局部化的Connection对象
*
* @return 返回当前线程上的Connection对象
* @throws DaoException
*/
public static synchronized Connection getConnection()throws DaoException{
Connection con=t1.get();
if(con==null){
try{
con=ds.getConnection();
t1.set(con);
}catch(SQLException ce){
throw new DaoException(ce);
}
}
return con;
}
/**
* 获取事务管理器
* @return
*/
public static synchronized TransactionManager getTranManager(){
return new TransactionManager(getConnection());
}
protected static void close(Connection conn) throws DaoException{
if(conn != null){
try {
conn.close();
t1.remove(); //卸装线程绑定
} catch (SQLException e) {
throw new DaoException("关闭连接时出现异常",e);
}
}
} public static String getURL(String s){
String s1 = "";
try{
URL url = new URL(s);
DataInputStream datainputstream = new DataInputStream(url.openConnection().getInputStream());
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
try{
byte abyte0[] = new byte[1024];
boolean flag = false;
do{
int i = datainputstream.read(abyte0);
if(i == -1)
break;
bytearrayoutputstream.write(abyte0, 0, i);
} while(true);
bytearrayoutputstream.flush();
byte abyte1[] = bytearrayoutputstream.toByteArray();
s1 = new String(abyte1, 0, abyte1.length, "GB2312");
}catch(Exception exception1){
exception1.printStackTrace();
}finally{
datainputstream.close();
bytearrayoutputstream.close();
}
}catch(Exception exception) { }
return s1;
}
}