81,119
社区成员
发帖
与我相关
我的任务
分享
static { try { Configuration cfg=new Configuration().configure(); //读取配置文件hibernate.cfg.xml sessionFactory=cfg.buildSessionFactory(); //创建SessionFactory } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } }
,configure("/hibernate.cfg.xml")呀!你可以试试
二月 25, 2014 1:11:45 下午 org.apache.catalina.core.StandardServer await
信息: A valid shutdown command was received via the shutdown port. Stopping the Server instance.
二月 25, 2014 1:11:45 下午 org.apache.coyote.AbstractProtocol pause
信息: Pausing ProtocolHandler ["http-bio-80"]
二月 25, 2014 1:11:45 下午 org.apache.coyote.AbstractProtocol pause
信息: Pausing ProtocolHandler ["ajp-bio-8009"]
二月 25, 2014 1:11:45 下午 org.apache.catalina.core.StandardService stopInternal
信息: Stopping service Catalina
二月 25, 2014 1:11:46 下午 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
严重: The web application [/ExtFirst] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@17f4438]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@57f0b1]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
二月 25, 2014 1:11:46 下午 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
严重: The web application [/ExtFirst] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@17f4438]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@17b1470]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
二月 25, 2014 1:11:46 下午 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
严重: The web application [/ExtFirst] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@17f4438]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@17b0672]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
二月 25, 2014 1:11:46 下午 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
严重: The web application [/ExtFirst] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@17f4438]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@1e45c78]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
二月 25, 2014 1:11:46 下午 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
严重: The web application [/ExtFirst] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@17f4438]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@ceafcb]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
二月 25, 2014 1:11:46 下午 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
严重: The web application [/ExtFirst] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@17f4438]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@177579d]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
二月 25, 2014 1:11:46 下午 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
严重: The web application [/ExtFirst] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@17f4438]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@1917a2]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
二月 25, 2014 1:11:46 下午 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
严重: The web application [/ExtFirst] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@17f4438]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@15eac87]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
二月 25, 2014 1:11:46 下午 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
严重: The web application [/ExtFirst] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@17f4438]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@910502]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
二月 25, 2014 1:11:46 下午 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
严重: The web application [/ExtFirst] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@17f4438]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@dfcdc6]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
二月 25, 2014 1:11:46 下午 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
严重: The web application [/ExtFirst] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@17f4438]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@13eacab]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
二月 25, 2014 1:11:46 下午 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextDestroyed()
二月 25, 2014 1:11:46 下午 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextDestroyed()
二月 25, 2014 1:11:46 下午 org.apache.coyote.AbstractProtocol stop
信息: Stopping ProtocolHandler ["http-bio-80"]
二月 25, 2014 1:11:46 下午 org.apache.coyote.AbstractProtocol stop
信息: Stopping ProtocolHandler ["ajp-bio-8009"]
二月 25, 2014 1:11:46 下午 org.apache.coyote.AbstractProtocol destroy
信息: Destroying ProtocolHandler ["http-bio-80"]
二月 25, 2014 1:11:46 下午 org.apache.coyote.AbstractProtocol destroy
信息: Destroying ProtocolHandler ["ajp-bio-8009"]
package lee;
public class DAOFactory {
public static UserDAO getUserDAOInstance(){
return new UserDAOImpl();
}
}
HibernateUtil.java
package lee;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static Configuration configuration = new Configuration();
//创建线程局部变量threadLocal,用来保存Hibernate的Session
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
//使用静态代码块初始化Hibernate
static {
try {
Configuration cfg=new Configuration().configure(); //读取配置文件hibernate.cfg.xml
sessionFactory=cfg.buildSessionFactory(); //创建SessionFactory
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
//获得SessionFactory实例
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
//获得ThreadLocal 对象管理的Session实例.
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
//通过SessionFactory对象创建Session对象
session = (sessionFactory != null) ? sessionFactory.openSession(): null;
//将新打开的Session实例保存到线程局部变量threadLocal中
threadLocal.set(session);
}
return session;
}
//关闭Session实例
public static void closeSession() throws HibernateException {
//从线程局部变量threadLocal中获取之前存入的Session实例
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
//重建SessionFactory
public static void rebuildSessionFactory() {
try {
configuration.configure("/hibernate.cfg.xml"); //读取配置文件hibernate.cfg.xml
sessionFactory = configuration.buildSessionFactory(); //创建SessionFactory
} catch (Exception e) {
System.err.println("Error Creating SessionFactory ");
e.printStackTrace();
}
}
//关闭缓存和连接池
public static void shutdown() {
getSessionFactory().close();
}
}
UserDAOImpl.java
package lee;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.*;
public class UserDAOImpl implements UserDAO {
//添加用户
public void save(User user){
Session session= HibernateUtil.getSession(); //生成Session实例
Transaction tx = session.beginTransaction(); //创建Transaction实例
try{
session.save(user); //使用Session的save方法将持久化对象保存到数据库
tx.commit(); //提交事务
} catch(Exception e){
e.printStackTrace();
tx.rollback(); //回滚事务
}finally{
HibernateUtil. closeSession(); //关闭Session实例
}
}
//根据用户标识查找指定用户
public User findById(int id){
User user=null;
Session session= HibernateUtil.getSession(); //生成Session实例
Transaction tx = session.beginTransaction(); //创建Transaction实例
try{
user=(User)session.get(User.class,id); //使用Session的get方法获取指定id的用户到内存中
tx.commit(); //提交事务
} catch(Exception e){
e.printStackTrace();
tx.rollback(); //回滚事务
}finally{
HibernateUtil. closeSession(); //关闭Session实例
}
return user;
}
//删除用户
public void delete(User user){
Session session= HibernateUtil.getSession(); //生成Session实例
Transaction tx = session.beginTransaction(); //创建Transaction实例
try{
session.delete(user); //使用Session的delete方法将持久化对象删除
tx.commit(); //提交事务
} catch(Exception e){
e.printStackTrace();
tx.rollback(); //回滚事务
}finally{
HibernateUtil. closeSession(); //关闭Session实例
}
}
//修改用户信息
public void update(User user){
Session session= HibernateUtil.getSession(); //生成Session实例
Transaction tx = session.beginTransaction(); //创建Transaction实例
try{
session.update(user); //使用Session的update方法更新持久化对象
tx.commit(); //提交事务
} catch(Exception e){
e.printStackTrace();
tx.rollback(); //回滚事务
}finally{
HibernateUtil. closeSession(); //关闭Session实例
}
}
}
UserAction.java
package lee;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
private boolean success;
private String message;
private User user;
public String execute() throws Exception {
if(user.getUsername().equals("ad")&&user.getPassword().equals("ad")){
UserDAO userdao = DAOFactory.getUserDAOInstance();
try{
User u = new User();
u.setId(5);
u.setUsername(user.getUsername());
u.setPassword(user.getPassword());
userdao.save(u);
this.success= true;
}catch(Exception e){
e.printStackTrace();
this.success=false;
this.message="对不起,未授权的用户不能登录改系统";
}
//this.message="你的账号是:"+user.getUsername()+"密码是:"+user.getPassword();
}else{
this.success=false;
this.message="对不起,未授权的用户不能登录改系统";
}
return SUCCESS;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}