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

本文介绍了在Spring框架中如何更新数据库配置的方法。当数据库配置发生变化时,可以通过重启系统、重建SessionFactory或重建Spring容器来实现。文章还提供了具体的代码示例说明如何安全地释放资源。
部署运行你感兴趣的模型镜像

数据库配置写在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();


 


 

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

dreamcode

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

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

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

打赏作者

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

抵扣说明:

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

余额充值