再就是如果数据库断了后,TOMCAT没办法自动重新连接数据库我应该怎么解决?谢谢大家<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://20.20.20.6:3306/tcs?useUnicode=true&characterEncoding=UTF-8</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123123</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- Mapping files -->
<mapping resource="com/tcs/bean/entitybean/TestRecord.hbm.xml" />
<mapping resource="com/tcs/bean/entitybean/StaticAnalyse.hbm.xml" />
</session-factory>
</hibernate-configuration>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://20.20.20.6:3306/tcs?useUnicode=true&characterEncoding=UTF-8</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123123</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- Mapping files -->
<mapping resource="com/tcs/bean/entitybean/TestRecord.hbm.xml" />
<mapping resource="com/tcs/bean/entitybean/StaticAnalyse.hbm.xml" />
</session-factory>
</hibernate-configuration>
解决方案 »
- struts2 的sessionmap问题
- 没人研究Jfreechart么?
- 有懂cewolf的吗,很简单的问题
- 全文搜索问题
- 一个案例实际问题,请高手指点。
- 关于分页显示的问题,高分求助
- JSP+Tomcat和JSP+IIS哪个功能强一点?
- 急急急!无法正确的引入ByteArrayDataSource.class这个包的存取地址!
- 请问高手如何处理邮件附件?我用JSP的setHeader方法来下载,可是文本文件时有问题啊?
- 求大神!!!!
- struts标签 property能否用array.length
- tomcat不能自动编译jsp文件的问题(好像是我把相关的服务停了或是删了文件)详情见内!
数据库断了,tomcat当然没办法自动重新连接,你要重新启动tomcat才能解决.
1、从http://proxool.sourceforge.Net/下载proxool,释放proxool.jar到WEB-INF/lib2、在hibernate.cfg.Xml中增加:
<property name="hibernate.proxool.pool_alias">dbpool</property>
<property name="hibernate.proxool.xml">proxool.xml</property>
<property name="connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</property>3、在与hibernate.cfg.xml同级目录(src根目录下)增加proxool.Xml文件:
<?Xml version="1.0" encoding="utf-8"?>
<!-- the proxool configuration can be embedded within your own application's.
Anything outside the "proxool" tag is ignored. -->
<something-else-entirely>
<proxool>
<alias>dbpool</alias>
<!--proxool只能管理由自己产生的连接-->
<driver-url>
jdbc:mysql://20.20.20.6:3306/tcs?useUnicode=true&characterEncoding=UTF-8
</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root" />
<property name="password" value="123123" />
</driver-properties>
<!-- proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁-->
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<!-- 最少保持的空闲连接数-->
<prototype-count>5</prototype-count>
<!-- 允许最大连接数,超过了这个连接,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定-->
<maximum-connection-count>100</maximum-connection-count>
<!-- 最小连接数-->
<minimum-connection-count>10</minimum-connection-count>
</proxool>
</something-else-entirely>4、重起tomcat
最近遇到的问题是客户数据库做集群.导致数据库切换的时候老是把连接断了,重起后过段时间又断了.
public class HibernateFactory {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
String s_ex = ex.getMessage();
System.out.print("[----->");
System.out.print("SessionFactory Load error:" + ex.getMessage());
System.out.print("<-----]");
throw new ExceptionInInitializerError(ex);
}
} public static final ThreadLocal session = new ThreadLocal(); public static Session currentSession() {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
} public static void closeSession() {
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
}
http://community.csdn.net/Expert/topic/5440/5440625.xml?temp=.2441675解决了立刻两个贴都结贴;
谢谢大家帮忙
建议最好不要去调用这个 closeSession().
http://218.246.23.46/UpBase/downloads/HibernateFactory.jar
我现在的版本是:
proxool-0.8.3.jar
hibernate3.jar
jdbc2_0-stdext.jar
mysql-connector-java-5.0.0-beta-bin.jar我那个版本不适合?
用法:
import com.liu.base.StaticHbmQuery;
import com.liu.base.StaticHbmSession;
import org.hibernate.Session;StaticHbmQuery shq = new StaticHbmQuery();
//StaticHbmQuery shq = new StaticHbmQuery("hibernate.cfg.xml"); //默认在程序根目录下
java.util.ArrayList arr = shq.HbmQuery("from table t"); //hibernate或sql查询语句
shq.HbmClear();
也可以
Session session = StaticHbmSession.currentSession();
//Session session = StaticHbmSession.currentChangeSession("hibernate.cfg.xml");
我使用了你给我发过来的包,但报的错和原来差不多:
FATAL connection.ProxoolConnectionProvider - Proxool Provider unable to load JAXP configurator file: proxool.xml好象是版本的问题
我现在的版本是:
proxool-0.8.3.jar
hibernate3.jar
jdbc2_0-stdext.jar
mysql-connector-java-5.0.0-beta-bin.jar
比如说 C3p0-0.8.5.2.jar 这个也是一个连接池.
hibernate.proxool.xml proxool.xml
hibernate.proxool.properties proxool.properties在hibernate.properties中,你是不是把这写文件放开了?
要是放开了就注释掉.
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.idle_test_period">100</property>
<property name="c3p0.max_size">100</property>
<property name="c3p0.max_statements">0</property>
<property name="c3p0.min_size">10</property>
<property name="c3p0.timeout">100</property>
可以在 my.ini 文件中启动项中添加 wait_timeout = 0
具体网上查一下
1、从http://proxool.sourceforge.Net/下载proxool,释放proxool.jar到WEB-INF/lib2、在hibernate.cfg.Xml中增加:
<property name="hibernate.proxool.pool_alias">dbpool</property>
<property name="hibernate.proxool.xml">proxool.xml</property>
<property name="connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</property>3、在与hibernate.cfg.xml同级目录(src根目录下)增加proxool.xml文件:
<?Xml version="1.0" encoding="utf-8"?>
<!-- the proxool configuration can be embedded within your own application's.
Anything outside the "proxool" tag is ignored. -->
<something-else-entirely>
<proxool>
<alias>dbpool</alias>
<!--proxool只能管理由自己产生的连接-->
<driver-url>
jdbc:mysql://20.20.20.6:3306/tcs?useUnicode=true&characterEncoding=UTF-8
</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root" />
<property name="password" value="123123" />
</driver-properties>
<!-- proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁-->
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<!-- 最少保持的空闲连接数-->
<prototype-count>5</prototype-count>
<!-- 允许最大连接数,超过了这个连接,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定-->
<maximum-connection-count>100</maximum-connection-count>
<!-- 最小连接数-->
<minimum-connection-count>10</minimum-connection-count>
</proxool>
</something-else-entirely>4、重起tomcat 这个方法我试了可以!!!
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.idle_test_period">100</property>
<property name="c3p0.max_size">100</property>
<property name="c3p0.max_statements">0</property>
<property name="c3p0.min_size">10</property>
<property name="c3p0.timeout">100</property>
另外,<Context path="..." docBase="..." debug="5" reloadable="false" crossContext="true"/>
reloadable="false"要注意,reload后,有的连接池会出现连接问题!