SSH(Struts+Spring+Hibernate)架构项目修改数据库密码但不重启应用的解决思路

数据库配置写在Spring配置文件里,因为Spring IOC容器中配置了sessionFactory Bean,它将随应用启动而加载,一旦Dao组件获得sessionFactory bean的引用就可以完成实际数据库访问。如果在应用运行中数据库配置发生改变就需要重新构建seesionFactory实例。

 

构建sessionFactory方法有三个,仅供参考:

1、重启系统

2、重建sessionfactory

Configuration config=Configuration config=new Configuration().configure();

SessionFactory sessionFactory=config.buildSessionFactory();

3、重建Spring容器

ClassPathXmlApplicationContext ctx = new ClaaPathApplicationContext(newString[]{"WEB-INF/ApplicationContext.xml"})

 

注意问题,重建sessionFactory需要首先做好释放

 

    /** 
     * 关闭当前的SessionFactory并且释放所有的资源 
     */  
    public static void shutdown()   
    {  
        log.debug("Shutting down Hibernate.");  
        // Close caches and connection pools  
        getSessionFactory().close();  
  
        // Clear static variables  
        configuration = null;  
        sessionFactory = null;  
    }  


 

     /** 
     * 使用指定的Configuration对象来重新构建SessionFactory对象。 
     * 
     * @param cfg 
     */  
     public static void rebuildSessionFactory(Configuration cfg)   
     {  
        log.debug("Rebuilding the SessionFactory from given Configuration.");  
        synchronized(sessionFactory)   
        {  
            if (sessionFactory != null && !sessionFactory.isClosed())  
                sessionFactory.close();  
            if (cfg.getProperty(Environment.SESSION_FACTORY_NAME) != null)  
                cfg.buildSessionFactory();  
            else  
                sessionFactory = cfg.buildSessionFactory();  
            configuration = cfg;  
        }  
     }  
 

 

释放Spring 容器:

AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 关闭Spring容器
 context.close();


 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dreamcode

你的鼓励是我创作的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值