- 浏览: 1127133 次
- 性别:
- 来自: 火星郊区
博客专栏
-
OSGi
浏览量:0
文章分类
- 全部博客 (695)
- 项目管理 (48)
- OSGi (122)
- java (79)
- Vaadin (5)
- RAP (47)
- mysql (40)
- Maven (22)
- SVN (8)
- 孔雀鱼 (10)
- hibernate (9)
- spring (10)
- css (3)
- 年审 (6)
- ant (1)
- jdbc (3)
- FusionCharts (2)
- struts (4)
- 决策分析 (2)
- 生活 (10)
- 架构设计 (5)
- 破解 (2)
- 狼文化 (4)
- JVM (14)
- J2EE (1)
- 应用服务器 (1)
- 我的链接 (5)
- 数学 (2)
- 报表 (1)
- 百科 (6)
- Flex (7)
- log4j (2)
- PHP (1)
- 系统 (2)
- Web前端 (7)
- linux (6)
- Office (1)
- 安全管理 (5)
- python (2)
- dom4j (1)
- 工作流 (3)
- 养生保健 (4)
- Eclipse (8)
- 监控开发 (1)
- 设计 (3)
- CAS (1)
- ZK (41)
- BluePrint (3)
- 工具 (1)
- SWT (7)
- google (2)
- NIO (1)
- 企业文化 (2)
- Windoes (0)
- RCP (7)
- JavaScript (10)
- UML (1)
- 产品经理 (2)
- Velocity (10)
- C (1)
- 单元测试 (1)
- 设计模式 (2)
- 系统分析师 (2)
- 架构 (4)
- 面试 (2)
- 代码走查 (1)
- MongoDB (1)
- 企业流程优化 (1)
- 模式 (1)
- EJB (1)
- Jetty (1)
- Git (13)
- IPV6 (1)
- JQuery (8)
- SSH (1)
- mybatis (10)
- SiteMesh (2)
- JSTL (1)
- veloctiy (1)
- Spring MVC (1)
- struts2 (3)
- Servlet (1)
- 权限管理 (1)
- Java Mina (1)
- java 系统信息 (6)
- OSGi 基础 (3)
- html (1)
- spring--security (6)
- HTML5 (1)
- java爬虫搜索 (1)
- mvc (3)
最新评论
-
Tom.X:
http://osgia.com/
将web容器置于OSGi框架下进行web应用的开发 -
chenyuguxing:
你好, 为什么我的bundle export到felix工程中 ...
在Apache Felix中运行bundle -
string2020:
<niceManifest>true</ni ...
Bundle Plugin for Maven -
jsonmong:
OSGI,是未来的主流,目前已相当成熟。应用OSGI比较好的, ...
基于OSGi的声明式服务 -
zyhui98:
貌似是翻译过来的,有很少人在linux上做开发吧
如何成为“10倍效率”开发者
Spring+Hibernate实现动态SessionFactory切换(改进版)
- 博客分类:
- spring
前面写了一篇关于动态切换Hibernate SessionFactory的文章,原文地址:http://tangyanbo.iteye.com/admin/blogs/1717402
发现存在一些问题:
需要配置多个HibernateTransactionManager和多个Spring 切面
这样带来两个问题
1. 程序效率降低,因为Spring进行多次Advice的拦截
2. 如果其中一个SessionFactory连接出现问题,会导致整个系统无法工作
今天研究出一种新的方法来解决此类问题
1. 数据源及Hibernate SessionFactory配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- FOR SqlServer-->
<bean id="SqlServer_DataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url"
value="url" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<bean id="SqlServer_SessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:mappingLocations="classpath:/com/entity/*.hbm.xml">
<property name="dataSource" ref="SqlServer_DataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<!-- FOR Oracle -->
<bean id="Oracle _DataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521/orcl" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<bean id="Oracle_SessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:mappingLocations="classpath:/com/entity/*.hbm.xml">
<property name="dataSource" ref="Oracle_DataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
</beans>
2. 定义扩展接口DynamicSessionFactoryInf继承SessionFactory
- import org.hibernate.SessionFactory;
- public interface DynamicSessionFactoryInf extends SessionFactory {
- public SessionFactory getHibernateSessionFactory();
- }
3. 定义DynamicSessionFactory实现DynamicSessionFactoryInf
- public class DynamicSessionFactory implements DynamicSessionFactoryInf ,ApplicationContextAware{
- private static final long serialVersionUID = 1L;
- private ApplicationContext applicationContext;
- //动态调用SessionFactory
- private SessionFactory getHibernateSessionFactory(String name) {
- return (SessionFactory) applicationContext.getBean(name);
- }
- //实现DynamicSessionFactoryInf 接口的方法
- public SessionFactory getHibernateSessionFactory() {
- return getHibernateSessionFactory(ThreadLocalUtil.getCurrentITAsset()
- .getSessionFactoryName());
- }
- //以下是实现SessionFactory接口的方法,并对当前的SessionFactory实体进行代理
- public Reference getReference() throws NamingException {
- return getHibernateSessionFactory().getReference();
- }
- public Session openSession() throws HibernateException {
- return getHibernateSessionFactory().openSession();
- }
- public Session openSession(Interceptor interceptor)
- throws HibernateException {
- return getHibernateSessionFactory().openSession(interceptor);
- }
- public Session openSession(Connection connection) {
- return getHibernateSessionFactory().openSession(connection);
- }
- public Session openSession(Connection connection, Interceptor interceptor) {
- return getHibernateSessionFactory().openSession(connection,interceptor);
- }
- public Session getCurrentSession() throws HibernateException {
- return getHibernateSessionFactory().getCurrentSession();
- }
- public StatelessSession openStatelessSession() {
- return getHibernateSessionFactory().openStatelessSession();
- }
- public StatelessSession openStatelessSession(Connection connection) {
- return getHibernateSessionFactory().openStatelessSession(connection);
- }
- public ClassMetadata getClassMetadata(Class entityClass) {
- return getHibernateSessionFactory().getClassMetadata(entityClass);
- }
- public ClassMetadata getClassMetadata(String entityName) {
- return getHibernateSessionFactory().getClassMetadata(entityName);
- }
- public CollectionMetadata getCollectionMetadata(String roleName) {
- return getHibernateSessionFactory().getCollectionMetadata(roleName);
- }
- public Map getAllClassMetadata() {
- return getHibernateSessionFactory().getAllClassMetadata();
- }
- public Map getAllCollectionMetadata() {
- return getHibernateSessionFactory().getAllCollectionMetadata();
- }
- public Statistics getStatistics() {
- return getHibernateSessionFactory().getStatistics();
- }
- public void close() throws HibernateException {
- getHibernateSessionFactory().close();
- }
- public boolean isClosed() {
- return getHibernateSessionFactory().isClosed();
- }
- public Cache getCache() {
- return getHibernateSessionFactory().getCache();
- }
- public void evict(Class persistentClass) throws HibernateException {
- getHibernateSessionFactory().evict(persistentClass);
- }
- public void evict(Class persistentClass, Serializable id)
- throws HibernateException {
- getHibernateSessionFactory().evict(persistentClass, id);
- }
- public void evictEntity(String entityName) throws HibernateException {
- getHibernateSessionFactory().evictEntity(entityName);
- }
- public void evictEntity(String entityName, Serializable id)
- throws HibernateException {
- getHibernateSessionFactory().evictEntity(entityName, id);
- }
- public void evictCollection(String roleName) throws HibernateException {
- getHibernateSessionFactory().evictCollection(roleName);
- }
- public void evictCollection(String roleName, Serializable id)
- throws HibernateException {
- getHibernateSessionFactory().evictCollection(roleName, id);
- }
- public void evictQueries(String cacheRegion) throws HibernateException {
- getHibernateSessionFactory().evictQueries(cacheRegion);
- }
- public void evictQueries() throws HibernateException {
- getHibernateSessionFactory().evictQueries();
- }
- public Set getDefinedFilterNames() {
- return getHibernateSessionFactory().getDefinedFilterNames();
- }
- public FilterDefinition getFilterDefinition(String filterName)
- throws HibernateException {
- return getHibernateSessionFactory().getFilterDefinition(filterName);
- }
- public boolean containsFetchProfileDefinition(String name) {
- return getHibernateSessionFactory().containsFetchProfileDefinition(name);
- }
- @Override
- public void setApplicationContext(ApplicationContext applicationContext)
- throws BeansException {
- this .applicationContext = applicationContext;
- }
- }
4. 配置动态SessionFactory
5. 定义DynamicTransactionManager继承HibernateTransactionManager
- public class DynamicTransactionManager extends HibernateTransactionManager {
- private static final long serialVersionUID = 1047039346475978451L;
- //重写getDataSource方法,实现动态获取
- public DataSource getDataSource() {
- DataSource sfds = SessionFactoryUtils.getDataSource(getSessionFactory());
- return sfds;
- }
- //重写getSessionFactory方法,实现动态获取SessionFactory
- public SessionFactory getSessionFactory() {
- DynamicSessionFactoryInf dynamicSessionFactory = (DynamicSessionFactoryInf) super
- .getSessionFactory();
- SessionFactory hibernateSessionFactory = dynamicSessionFactory
- .getHibernateSessionFactory();
- return hibernateSessionFactory;
- }
- //重写afterPropertiesSet,跳过数据源的初始化等操作
- public void afterPropertiesSet() {
- return ;
- }
- }
6. 配置dynamicTransactionManager
<bean id="dynamicTransactionManager" class="com.hp.it.qdpadmin.common.DynamicTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean>
7. 为SessionFactory配置事务切面
<tx:advice id="dynamicTxAdvice" transaction-manager="dynamicTransactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="find*" read-only="true" /> <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" /> </tx:attributes> </tx:advice> <aop:config proxy-target-class="true"> <aop:pointcut id="txPointcut" expression="execution(* com.service.*.*(..))"/> <aop:advisor advice-ref="dynamicTxAdvice" pointcut-ref="txPointcut" /> </aop:config>
发表评论
-
深入理解Spring MVC 3(三)
2013-07-20 19:30 1427十二、如何把全局异常记录到日志中? 在 前的配置中,其中有 ... -
深入理解Spring MVC 3(二)
2013-07-20 19:29 1279要写在DispatcherServlet的前面, 让 def ... -
深入理解Spring MVC 3(一)
2013-07-20 19:28 1671一、前言: 大家好,Spring3 MVC是非常优秀的MV ... -
Spring2.5中使用注解装配属性and组件自动扫描
2012-10-18 11:22 1050Spring2.5中使用注解装配属性 可在Jav ... -
spring中加载Bean配置文件的常用方式
2012-10-18 09:07 1206Spring中加载Bean配置文件的常用方式有两种,一种是通过 ... -
Spring常用注解,自动扫描装配Bean
2012-10-16 08:40 19421 引入context命名空间(在Spring的配置文件中), ... -
Spring Bean的5种作用域
2012-10-16 08:40 1063Spring Bean的5种作用域 ... -
初识 Spring 框架,进入Spring的世界
2012-10-19 10:15 1006一、SpringSource公司 S ... -
spring各jar包相着依赖关系
2011-03-25 08:21 1139spring.jar是包含有完整发布的单个jar包,s ...
相关推荐
在spring+hibernate的框架中定时操作数据库,主要是拿到sessionFactory,不会出现no session 和transaction no-bound等问题,由sessionFactory完成对数据的操作,有些包是没有用的,有兴趣的可以自己删除掉
12.6 整合Struts、Spring和Hibernate实现用户管理 12.6.1 Struts、Spring和Hibernate的整合方式 12.6.2 编写用户注册画面regedit.jsp 12.6.3 编写用户登录画面login.jsp 12.6.4 编写注册控制器RegeditAction.java ...
Spring4.0+Hibernate4.0+Struts2.3整合案例:实现增删改查。 ===================== application.xml: xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...
2.Spring+Jotm整合实现JTA分布式事务,应用场景如转账等,同一事务内完成db1用户加100元、db2用户减100元。 3.Spring+Junit4单元测试,优点:不会破坏数据库现场,等等。 (特别注意:Spring3.0里不在提供对jotm的...
Struts2+Hibernate+Spring整合实例,登陆注册实例,简单来说,Spring通过IoC容器上管(Struts2)Action的创建并依赖注入给控制器,下管(hibernate)SessionFactory的创建并依赖注入给DAO组件,是一个巨大的工厂
一个实例小工程,讲解的是将hibernate的sessionFactory交给spring管理的配置方法
12.6 整合Struts、Spring和Hibernate实现用户管理 12.6.1 Struts、Spring和Hibernate的整合方式 12.6.2 编写用户注册画面regedit.jsp 12.6.3 编写用户登录画面login.jsp 12.6.4 编写注册控制器RegeditAction.java ...
12.6 整合Struts、Spring和Hibernate实现用户管理 12.6.1 Struts、Spring和Hibernate的整合方式 12.6.2 编写用户注册画面regedit.jsp 12.6.3 编写用户登录画面login.jsp 12.6.4 编写注册控制器RegeditAction.java ...
前几天搞 Spring + Hibernate + Struts 事务配置 ,网上找了好多资料,不过好无语,大多都是 Ctrl + V,浪费俺的宝贵时间 现在我总结配出一套,给大家参考参考,可能有不足,请大家多多交流。 附:内有弹出...
# 不熟悉git的同学也没关系, 根据不同搭建进度, 资源内分别打了三个项目(springmvc、springmvc+spring、springmvc+spring+hibernate), 可逐个对比查看; # 项目主要描述ssh的搭建步骤, 前端仅仅是几个简单的展示页面;...
基于三大框架架构Spring+Struts2+Hibernate 这个是我毕业设计,有论文,也有答辩ppt,开题报告... 比较成功,所以传上来与大家分享。 界面采用新浪界面样式, 后台层次非常清晰。 数据库采用MySql。 服务器用tomcat...
其中Hibernate每次都需要手动创建SessionFactory,Session,手动开启提交关闭事务。而这一切操作完全是由Spring来代替。使持久层更加方便,使开发人员减少持久层操作,把注意力放到业务上。
在SSH中实现以下功能时, 需要将Tomcat中站点目录下/WEB-INF/lib 中的asm-2.2.3.jar和commons-collections-2.1.1.jar 文件删除,否则会引起类冲突 分离配置文件: 在src目录下创建多个xml文件,分别配制项目中的 ...
这是一个spring2.5+struts2+hibernate3.3的整合完整项目,struts2的ActionBean,hibernate的sessionFactory交与了spring容器创建与管理
两种配置:oracle mysql,切换数据库只要把SessionFactory的配置文件改成对应就可以了 c3p0配置:mysql调试通过,oracle由于存在问题,未配置 spring配置式事务管理(jdk动态代理,每个service必须对应一个接口) ...
一个spring2.5+hibernate3.3+struts1.3的整合的完整项目,该项目使用spring容器创建sessionFactory,管理struts ActionBean的创建,其中使用注解的方式创建实体Bean以及依赖注入和事务
基于SSH的Java EE新闻发布系统项目介绍基于Struts + Spring + Hibernate + Bootstrap技术开发的新闻发布系统。系统功能:前台为管理员对新闻以及新闻栏目的CRUD操作,新闻审核,权限以及角色的控制等。技术选型初步...
Myeclipse开发struts+hibernate+spring新手入门---环境配置----项目开发示例 Myeclipse开发struts+hibernate+spring小记 开发前准备工作: 1、下载eclipse3.1版本 下载地址: 2、下载Myeclipse插件 下载地址: 3...
1.Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Sesssion 4.创建事务Transation 5.持久化操作 6.提交事务 7.关闭Session 8.关闭...
Hibernate中的sessionFactory