`

osgi spring hibernate

    博客分类:
  • OSGI
阅读更多

 先来说说整合Hibernate的关键之处。其实用OSGi整合Hibernate很简单,但要通过Bundle方式做到可以扩展新的持久化层面的东西(比如添加新的表和操作)就比较费事了。因为Hibernate在初始化时根据注册的实体类创建SessionFactory,这样当有新的实体类添加进来时就要创建新的SessionFactory,这样系统中出现两个甚至多个SessionFatory会导致一系列的问题。显然整合Hibernate关键就是解决实体类注册与SessionFactory创建的问题。
        我的具体思路如下。
        首先将Hibernate单独多为一个Bundle(wanged_commons_hibernate)以便提供其他Bundle所需类包。
        然后建立一个用于提供实体注册接口的Bundle(wanged_core_persistent_entity_register),代码如下:

java 代码
 
  1. package wanged.core.persistent.entity;  
  2.   
  3. @SuppressWarnings("unchecked")  
  4. public interface EntityRegister {  
  5.   
  6.     /** 
  7.      * 注册Hibernate的实体Class 
  8.      * @return 
  9.      */  
  10.     Class[] register();  
  11.      
  12. }  

       建一个用来初始化SessionFactory和事务管理的Bundle(wanged_core_persistent),由于使用Spring提供的LocalSessionFactoryBean会有问题,所以我单独写了两个类。
LocalSessionFactoryBean的代码:

java 代码
 
  1. package wanged.core.persistent;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Properties;  
  5.   
  6. import javax.sql.DataSource;  
  7.   
  8. import org.hibernate.ConnectionReleaseMode;  
  9. import org.hibernate.HibernateException;  
  10. import org.hibernate.SessionFactory;  
  11. import org.hibernate.cfg.Configuration;  
  12. import org.hibernate.cfg.Environment;  
  13. import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;  
  14. import org.springframework.orm.hibernate3.AbstractSessionFactoryBean;  
  15. import org.springframework.orm.hibernate3.SpringSessionContext;  
  16. import org.springframework.orm.hibernate3.TransactionAwareDataSourceConnectionProvider;  
  17.   
  18. import wanged.core.persistent.entity.EntityRegister;  
  19.   
  20. @SuppressWarnings("unchecked")  
  21. public class LocalSessionFactoryBean extends AbstractSessionFactoryBean {  
  22.   
  23.     private static final ThreadLocal configTimeDataSourceHolder = new ThreadLocal();  
  24.   
  25.     private Properties hibernateProperties;  
  26.   
  27.     private HashMapnew HashMap
  28.   
  29.     private Configuration configuration;  
  30.   
  31.     public static DataSource getConfigTimeDataSource() {  
  32.         return (DataSource) configTimeDataSourceHolder.get();  
  33.     }  
  34.   
  35.     /** 
  36.      * 注册Entity的Class数组 
  37.      *  
  38.      * @param er 
  39.      */  
  40.     @SuppressWarnings("unchecked")  
  41.     public void setEntityRegister(EntityRegister[] erArr) {  
  42.         for (EntityRegister er : erArr) {  
  43.             this.addEntityRegister(er);  
  44.         }  
  45.   
  46.     }  
  47.       
  48.     @SuppressWarnings("unchecked")  
  49.     public void setEntityRegister(EntityRegister er) {  
  50.         this.addEntityRegister(er);  
  51.     }  
  52.   
  53.     private void addEntityRegister(EntityRegister er){  
  54.         // TODO:对registerClass()中取得的数组进行验证  
  55.         this.entityClasses.put(er, er.register());  
  56.     }  
  57.       
  58.     /** 
  59.      * 卸载Entity的Class数组 
  60.      *  
  61.      * @param er 
  62.      */  
  63.     public void unsetEntityRegister(EntityRegister er) {  
  64.         this.entityClasses.remove(er);  
  65.   
  66.         // TODO:重新初始化SessionFactory  
  67.   
  68.     }  
  69.   
  70.     public void setHibernateProperties(Properties hibernateProperties) {  
  71.         this.hibernateProperties = hibernateProperties;  
  72.     }  
  73.   
  74.     public Properties getHibernateProperties() {  
  75.         if (this.hibernateProperties == null) {  
  76.             this.hibernateProperties = new Properties();  
  77.         }  
  78.         return this.hibernateProperties;  
  79.     }  
  80.   
  81.     @SuppressWarnings("unchecked")  
  82.     protected SessionFactory buildSessionFactory() {  
  83.         Configuration config = new Configuration();  
  84.         DataSource dataSource = getDataSource();  
  85.         if (dataSource != null) {  
  86.             configTimeDataSourceHolder.set(dataSource);  
  87.         }  
  88.   
  89.         try {  
  90.             config.setProperty(Environment.RELEASE_CONNECTIONS, ConnectionReleaseMode.ON_CLOSE.toString());  
  91.   
  92.             if (isExposeTransactionAwareSessionFactory()) {  
  93.                 config.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName());  
  94.             }  
  95.   
  96.             if (this.hibernateProperties != null) {  
  97.                 config.addProperties(this.hibernateProperties);  
  98.             }  
  99.   
  100.             if (dataSource != null) {  
  101.                 boolean actuallyTransactionAware = (isUseTransactionAwareDataSource() || dataSource instanceof TransactionAwareDataSourceProxy);  
  102.                 config.setProperty(Environment.CONNECTION_PROVIDER, actuallyTransactionAware ? TransactionAwareDataSourceConnectionProvider.class  
  103.                                 .getName() : LocalDataSourceConnectionProvider.class.getName());  
  104.             }  
  105.   
  106.             // 添加Entity的类  
  107.             for (Class[] cArr : this.entityClasses.values()) {  
  108.                 for (Class c : cArr) {  
  109.                     config.addClass(c);  
  110.                 }  
  111.             }  
  112.             this.configuration = config;  
  113.             return config.buildSessionFactory();  
  114.         } finally {  
  115.             if (dataSource != null) {  
  116.                 configTimeDataSourceHolder.set(null);  
  117.             }  
  118.         }  
  119.     }  
  120.   
  121.     /** 
  122.      * Return the Configuration object used to build the SessionFactory. Allows 
  123.      * access to configuration metadata stored there (rarely needed). 
  124.      *  
  125.      * @throws IllegalStateException 
  126.      *             if the Configuration object has not been initialized yet 
  127.      */  
  128.     public final Configuration getConfiguration() {  
  129.         if (this.configuration == null) {  
  130.             throw new IllegalStateException("Configuration not initialized yet");  
  131.         }  
  132.         return this.configuration;  
  133.     }  
  134.   
  135.     public void destroy() throws HibernateException {  
  136.         DataSource dataSource = getDataSource();  
  137.         if (dataSource != null) {  
  138.             configTimeDataSourceHolder.set(dataSource);  
  139.         }  
  140.         try {  
  141.             super.destroy();  
  142.         } finally {  
  143.             if (dataSource != null) {  
  144.                 configTimeDataSourceHolder.set(null);  
  145.             }  
  146.         }  
  147.     }  
  148. }  

LocalDataSourceConnectionProvider的代码:

java 代码
引用 "实体注册" 服务需要在我们的实体Bundle中实现,现在已经搭好了架子,下一步就需要创建自己的实体Bundle和数据库操作的Bundle。由于这个Blog保存时经常保存不住,需要重写,所以我尽可能的将其划分开来写。

 
  1. package wanged.core.persistent;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.SQLException;  
  5. import java.util.Properties;  
  6.   
  7. import javax.sql.DataSource;  
  8.   
  9. import org.hibernate.HibernateException;   先来说说整合Hibernate的关键之处。其实用OSGi整合Hibernate很简单,但要通过Bundle方式做到可以扩展新的持久化层面的东西(比如添加新的表和操作)就比较费事了。因为Hibernate在初始化时根据注册的实体类创建SessionFactory,这样当有新的实体类添加进来时就要创建新的SessionFactory,这样系统中出现两个甚至多个SessionFatory会导致一系列的问题。显然整合Hibernate关键就是解决实体类注册与SessionFactory创建的问题。 我的具体思路如下。 首先将Hibernate单独多为一个Bundle(wanged_commons_hibernate)以便提供其他Bundle所需类包。 然后建立一个用于提供实体注册接口的Bundle(wanged_core_persistent_entity_register),代码如下: java 代码 package wanged.core.persistent.entity; @SuppressWarnings("unchecked") public interface EntityRegister { /** * 注册Hibernate的实体Class * @return */ Class[] register(); } 建一个用来初始化SessionFactory和事务管理的Bundle(wanged_core_persistent),由于使用Spring提供的LocalSessionFactoryBean会有问题,所以我单独写了两个类。类LocalSessionFactoryBean的代码: java 代码 package wanged.core.persistent; import java.util.HashMap; import java.util.Properties; import javax.sql.DataSource; import org.hibernate.ConnectionReleaseMode; import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; import org.springframework.orm.hibernate3.AbstractSessionFactoryBean; import org.springframework.orm.hibernate3.SpringSessionContext; import org.springframework.orm.hibernate3.TransactionAwareDataSourceConnectionProvider; import wanged.core.persistent.entity.EntityRegister; @SuppressWarnings("unchecked") public class LocalSessionFactoryBean extends AbstractSessionFactoryBean { private static final ThreadLocal configTimeDataSourceHolder = new ThreadLocal(); private Properties hibernateProperties; private HashMapnew HashMap private Configuration configuration; public static DataSource getConfigTimeDataSource() { return (DataSource) configTimeDataSourceHolder.get(); } /** * 注册Entity的Class数组 * * @param er */ @SuppressWarnings("unchecked") public void setEntityRegister(EntityRegister[] erArr) { for (EntityRegister er : erArr) { this.addEntityRegister(er); } } @SuppressWarnings("unchecked") public void setEntityRegister(EntityRegister er) { this.addEntityRegister(er); } private void addEntityRegister(EntityRegister er){ // TODO:对registerClass()中取得的数组进行验证 this.entityClasses.put(er, er.register()); } /** * 卸载Entity的Class数组 * * @param er */ public void unsetEntityRegister(EntityRegister er) { this.entityClasses.remove(er); // TODO:重新初始化SessionFactory } public void setHibernateProperties(Properties hibernateProperties) { this.hibernateProperties = hibernateProperties; } public Properties getHibernateProperties() { if (this.hibernateProperties == null) { this.hibernateProperties = new Properties(); } return this.hibernateProperties; } @SuppressWarnings("unchecked") protected SessionFactory buildSessionFactory() { Configuration config = new Configuration(); DataSource dataSource = getDataSource(); if (dataSource != null) { configTimeDataSourceHolder.set(dataSource); } try { config.setProperty(Environment.RELEASE_CONNECTIONS, ConnectionReleaseMode.ON_CLOSE.toString()); if (isExposeTransactionAwareSessionFactory()) { config.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName()); } if (this.hibernateProperties != null) { config.addProperties(this.hibernateProperties); } if (dataSource != null) { boolean actuallyTransactionAware = (isUseTransactionAwareDataSource() || dataSource instanceof TransactionAwareDataSourceProxy); config.setProperty(Environment.CONNECTION_PROVIDER, actuallyTransactionAware ? TransactionAwareDataSourceConnectionProvider.class .getName() : LocalDataSourceConnectionProvider.class.getName()); } // 添加Entity的类 for (Class[] cArr : this.entityClasses.values()) { for (Class c : cArr) { config.addClass(c); } } this.configuration = config; return config.buildSessionFactory(); } finally { if (dataSource != null) { configTimeDataSourceHolder.set(null); } } } /** * Return the Configuration object used to build the SessionFactory. Allows * access to configuration metadata stored there (rarely needed). * * @throws IllegalStateException * if the Configuration object has not been initialized yet */ public final Configuration getConfiguration() { if (this.configuration == null) { throw new IllegalStateException("Configuration not initialized yet"); } return this.configuration; } public void destroy() throws HibernateException { DataSource dataSource = getDataSource(); if (dataSource != null) { configTimeDataSourceHolder.set(dataSource); } try { super.destroy(); } finally { if (dataSource != null) { configTimeDataSourceHolder.set(null); } } } } 类LocalDataSourceConnectionProvider的代码: java 代码 package wanged.core.persistent; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; import javax.sql.DataSource; import org.hibernate.HibernateException; import org.hibernate.connection.ConnectionProvider; import org.hibernate.util.JDBCExceptionReporter; public class LocalDataSourceConnectionProvider implements ConnectionProvider { private DataSource dataSource; private DataSource dataSourceToUse; public void configure(Properties props) throws HibernateException { this.dataSource = LocalSessionFactoryBean.getConfigTimeDataSource(); if (this.dataSource == null) { throw new HibernateException("No local DataSource found for configuration - " + "dataSource property must be set on LocalSessionFactoryBean"); } this.dataSourceToUse = getDataSourceToUse(this.dataSource); } protected DataSource getDataSourceToUse(DataSource originalDataSource) { return originalDataSource; } public DataSource getDataSource() { return dataSource; } public Connection getConnection() throws SQLException { try { return this.dataSourceToUse.getConnection(); } catch (SQLException ex) { JDBCExceptionReporter.logExceptions(ex); throw ex; } } public void closeConnection(Connection con) throws SQLException { try { con.close(); } catch (SQLException ex) { JDBCExceptionReporter.logExceptions(ex); throw ex; } } public void close() { } public boolean supportsAggressiveRelease() { return false; } } 如果你对比其与Spring提供的同名类中的代码,相差不大。下面来看看配置文件,我把Bean的初始化放在bean.xml中: xml 代码 <!-- 本地调试使用连接池--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.gjt.mm.mysql.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/cms" /> <property name="username" value="root" /> <property name="password" value="root" /> <property name="connectionProperties"> <props> <prop key="useUnicode">true</prop> <prop key="characterEncoding">GBK</prop> </props> </property> </bean> <!-- 服务实现类定义 --> <bean id="sessionFactory" class="wanged.core.persistent.LocalSessionFactoryBean"> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.jdbc.batch_size">20</prop> </props> </property> <property name="dataSource" ref="dataSource" /> <property name="entityRegister" ref="entityRegister" /> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> 而服务与引用的声明放在osgi-service.xml中: xml 代码 <osgi:reference id="entityRegister" interface="wanged.core.persistent.entity.EntityRegister" cardinality="1..n"/> <osgi:service interface="org.hibernate.SessionFactory" ref="sessionFactory" /> <osgi:service interface="org.springframework.transaction.PlatformTransactionManager" ref="txManager" /> 这里的引用 "实体注册" 服务需要在我们的实体Bundle中实现,现在已经搭好了架子,下一步就需要创建自己的实体Bundle和数据库操作的Bundle。由于这个Blog保存时经常保存不住,需要重写,所以我尽可能的将其划分开来写。
    
     
  10. import org.hibernate.connection.ConnectionProvider;  
  11. import org.hibernate.util.JDBCExceptionReporter;  
  12.   
  13. public class LocalDataSourceConnectionProvider implements ConnectionProvider {  
  14.   
  15.     private DataSource dataSource;  
  16.   
  17.     private DataSource dataSourceToUse;  
  18.   
  19.   
  20.     public void configure(Properties props) throws HibernateException {  
  21.         this.dataSource = LocalSessionFactoryBean.getConfigTimeDataSource();  
  22.         if (this.dataSource == null) {  
  23.             throw new HibernateException("No local DataSource found for configuration - " +  
  24.                 "dataSource property must be set on LocalSessionFactoryBean");  
  25.         }  
  26.         this.dataSourceToUse = getDataSourceToUse(this.dataSource);  
  27.     }  
  28.   
  29.     protected DataSource getDataSourceToUse(DataSource originalDataSource) {  
  30.         return originalDataSource;  
  31.     }  
  32.   
  33.     public DataSource getDataSource() {  
  34.         return dataSource;  
  35.     }  
  36.   
  37.     public Connection getConnection() throws SQLException {  
  38.         try {  
  39.             return this.dataSourceToUse.getConnection();  
  40.         }  
  41.         catch (SQLException ex) {  
  42.             JDBCExceptionReporter.logExceptions(ex);  
  43.             throw ex;  
  44.         }  
  45.     }  
  46.   
  47.     public void closeConnection(Connection con) throws SQLException {  
  48.         try {  
  49.             con.close();  
  50.         }  
  51.         catch (SQLException ex) {  
  52.             JDBCExceptionReporter.logExceptions(ex);  
  53.             throw ex;  
  54.         }  
  55.     }  
  56.   
  57.     public void close() {  
  58.     }  
  59.   
  60.     public boolean supportsAggressiveRelease() {  
  61.         return false;  
  62.     }  
  63.   
  64. }  

如果你对比其与Spring提供的同名类中的代码,相差不大。
下面来看看配置文件,我把Bean的初始化放在bean.xml中:

xml 代码
 
  1. <!-- 本地调试使用连接池-->  
  2.   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  3.     <property name="driverClassName" value="org.gjt.mm.mysql.Driver" />  
  4.     <property name="url" value="jdbc:mysql://localhost:3306/cms" />  
  5.     <property name="username" value="root" />  
  6.     <property name="password" value="root" />  
  7.     <property name="connectionProperties">  
  8.       <props>  
  9.         <prop key="useUnicode">true</prop>  
  10.         <prop key="characterEncoding">GBK</prop>  
  11.       </props>  
  12.     </property>  
  13.   </bean>  
  14.    
  15.   <!-- 服务实现类定义 -->  
  16.   <bean id="sessionFactory" class="wanged.core.persistent.LocalSessionFactoryBean">  
  17.     <property name="hibernateProperties">  
  18.       <props>  
  19.         <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
  20.         <prop key="hibernate.show_sql">false</prop>  
  21.         <prop key="hibernate.cache.use_query_cache">true</prop>  
  22.         <prop key="hibernate.jdbc.batch_size">20</prop>  
  23.       </props>  
  24.     </property>  
  25.     <property name="dataSource" ref="dataSource" />  
  26.     <property name="entityRegister" ref="entityRegister" />  
  27.   </bean>  
  28.   
  29.   <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  30.     <property name="sessionFactory" ref="sessionFactory" />  
  31.   </bean>  

而服务与引用的声明放在osgi-service.xml中:
xml 代码
 
  1. <osgi:reference id="entityRegister"  interface="wanged.core.persistent.entity.EntityRegister" cardinality="1..n"/>    
  2.   
  3. <osgi:service interface="org.hibernate.SessionFactory" ref="sessionFactory" />    
  4.   
  5. <osgi:service interface="org.springframework.transaction.PlatformTransactionManager" ref="txManager" />    
这里的
分享到:
评论
2 楼 wxjiaaa 2009-12-23  
package wanged.core.persistent.entity; @SuppressWarnings("unchecked") public interface EntityRegister { /** * 注册Hibernate的实体Class * @return */ Class[] register(); } 建一个用来初始化SessionFactory和事务管理的Bundle(wanged_core_persistent),由于使用Spring提供的LocalSessionFactoryBean会有问题,所以我单独写了两个类。类LocalSessionFactoryBean的代码: java 代码 package wanged.core.persistent; import java.util.HashMap; import java.util.Properties; import javax.sql.DataSource; import org.hibernate.ConnectionReleaseMode; import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; import org.springframework.orm.hibernate3.AbstractSessionFactoryBean; import org.springframework.orm.hibernate3.SpringSessionContext; import org.springframework.orm.hibernate3.TransactionAwareDataSourceConnectionProvider; import wanged.core.persistent.entity.EntityRegister; @SuppressWarnings("unchecked") public class LocalSessionFactoryBean extends AbstractSessionFactoryBean { private static final ThreadLocal configTimeDataSourceHolder = new ThreadLocal(); private Properties hibernateProperties; private HashMapnew HashMap private Configuration configuration; public static DataSource getConfigTimeDataSource() { return (DataSource) configTimeDataSourceHolder.get(); } /** * 注册Entity的Class数组 * * @param er */ @SuppressWarnings("unchecked") public void setEntityRegister(EntityRegister[] erArr) { for (EntityRegister er : erArr) { this.addEntityRegister(er); } } @SuppressWarnings("unchecked") public void setEntityRegister(EntityRegister er) { this.addEntityRegister(er); } private void addEntityRegister(EntityRegister er){ // TODO:对registerClass()中取得的数组进行验证 this.entityClasses.put(er, er.register()); } /** * 卸载Entity的Class数组 * * @param er */ public void unsetEntityRegister(EntityRegister er) { this.entityClasses.remove(er); // TODO:重新初始化SessionFactory } public void setHibernateProperties(Properties hibernateProperties) { this.hibernateProperties = hibernateProperties; } public Properties getHibernateProperties() { if (this.hibernateProperties == null) { this.hibernateProperties = new Properties(); } return this.hibernateProperties; } @SuppressWarnings("unchecked") protected SessionFactory buildSessionFactory() { Configuration config = new Configuration(); DataSource dataSource = getDataSource(); if (dataSource != null) { configTimeDataSourceHolder.set(dataSource); } try { config.setProperty(Environment.RELEASE_CONNECTIONS, ConnectionReleaseMode.ON_CLOSE.toString()); if (isExposeTransactionAwareSessionFactory()) { config.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName()); } if (this.hibernateProperties != null) { config.addProperties(this.hibernateProperties); } if (dataSource != null) { boolean actuallyTransactionAware = (isUseTransactionAwareDataSource() || dataSource instanceof TransactionAwareDataSourceProxy); config.setProperty(Environment.CONNECTION_PROVIDER, actuallyTransactionAware ? TransactionAwareDataSourceConnectionProvider.class .getName() : LocalDataSourceConnectionProvider.class.getName()); } // 添加Entity的类 for (Class[] cArr : this.entityClasses.values()) { for (Class c : cArr) { config.addClass(c); } } this.configuration = config; return config.buildSessionFactory(); } finally { if (dataSource != null) { configTimeDataSourceHolder.set(null); } } } /** * Return the Configuration object used to build the SessionFactory. Allows * access to configuration metadata stored there (rarely needed). * * @throws IllegalStateException * if the Configuration object has not been initialized yet */ public final Configuration getConfiguration() { if (this.configuration == null) { throw new IllegalStateException("Configuration not initialized yet"); } return this.configuration; } public void destroy() throws HibernateException { DataSource dataSource = getDataSource(); if (dataSource != null) { configTimeDataSourceHolder.set(dataSource); } try { super.destroy(); } finally { if (dataSource != null) { configTimeDataSourceHolder.set(null); } } } } 类LocalDataSourceConnectionProvider的代码: java 代码 package wanged.core.persistent; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; import javax.sql.DataSource; import org.hibernate.HibernateException; import org.hibernate.connection.ConnectionProvider; import org.hibernate.util.JDBCExceptionReporter; public class LocalDataSourceConnectionProvider implements ConnectionProvider { private DataSource dataSource; private DataSource dataSourceToUse; public void configure(Properties props) throws HibernateException { this.dataSource = LocalSessionFactoryBean.getConfigTimeDataSource(); if (this.dataSource == null) { throw new HibernateException("No local DataSource found for configuration - " + "dataSource property must be set on LocalSessionFactoryBean"); } this.dataSourceToUse = getDataSourceToUse(this.dataSource); } protected DataSource getDataSourceToUse(DataSource originalDataSource) { return originalDataSource; } public DataSource getDataSource() { return dataSource; } public Connection getConnection() throws SQLException { try { return this.dataSourceToUse.getConnection(); } catch (SQLException ex) { JDBCExceptionReporter.logExceptions(ex); throw ex; } } public void closeConnection(Connection con) throws SQLException { try { con.close(); } catch (SQLException ex) { JDBCExceptionReporter.logExceptions(ex); throw ex; } } public void close() { } public boolean supportsAggressiveRelease() { return false; } }
1 楼 wxjiaaa 2009-12-23  
package wanged.core.persistent.entity; @SuppressWarnings("unchecked") public interface EntityRegister { /** * 注册Hibernate的实体Class * @return */ Class[] register(); } 建一个用来初始化SessionFactory和事务管理的Bundle(wanged_core_persistent),由于使用Spring提供的LocalSessionFactoryBean会有问题,所以我单独写了两个类。类LocalSessionFactoryBean的代码: java 代码 package wanged.core.persistent; import java.util.HashMap; import java.util.Properties; import javax.sql.DataSource; import org.hibernate.ConnectionReleaseMode; import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; import org.springframework.orm.hibernate3.AbstractSessionFactoryBean; import org.springframework.orm.hibernate3.SpringSessionContext; import org.springframework.orm.hibernate3.TransactionAwareDataSourceConnectionProvider; import wanged.core.persistent.entity.EntityRegister; @SuppressWarnings("unchecked") public class LocalSessionFactoryBean extends AbstractSessionFactoryBean { private static final ThreadLocal configTimeDataSourceHolder = new ThreadLocal(); private Properties hibernateProperties; private HashMapnew HashMap private Configuration configuration; public static DataSource getConfigTimeDataSource() { return (DataSource) configTimeDataSourceHolder.get(); } /** * 注册Entity的Class数组 * * @param er */ @SuppressWarnings("unchecked") public void setEntityRegister(EntityRegister[] erArr) { for (EntityRegister er : erArr) { this.addEntityRegister(er); } } @SuppressWarnings("unchecked") public void setEntityRegister(EntityRegister er) { this.addEntityRegister(er); } private void addEntityRegister(EntityRegister er){ // TODO:对registerClass()中取得的数组进行验证 this.entityClasses.put(er, er.register()); } /** * 卸载Entity的Class数组 * * @param er */ public void unsetEntityRegister(EntityRegister er) { this.entityClasses.remove(er); // TODO:重新初始化SessionFactory } public void setHibernateProperties(Properties hibernateProperties) { this.hibernateProperties = hibernateProperties; } public Properties getHibernateProperties() { if (this.hibernateProperties == null) { this.hibernateProperties = new Properties(); } return this.hibernateProperties; } @SuppressWarnings("unchecked") protected SessionFactory buildSessionFactory() { Configuration config = new Configuration(); DataSource dataSource = getDataSource(); if (dataSource != null) { configTimeDataSourceHolder.set(dataSource); } try { config.setProperty(Environment.RELEASE_CONNECTIONS, ConnectionReleaseMode.ON_CLOSE.toString()); if (isExposeTransactionAwareSessionFactory()) { config.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName()); } if (this.hibernateProperties != null) { config.addProperties(this.hibernateProperties); } if (dataSource != null) { boolean actuallyTransactionAware = (isUseTransactionAwareDataSource() || dataSource instanceof TransactionAwareDataSourceProxy); config.setProperty(Environment.CONNECTION_PROVIDER, actuallyTransactionAware ? TransactionAwareDataSourceConnectionProvider.class .getName() : LocalDataSourceConnectionProvider.class.getName()); } // 添加Entity的类 for (Class[] cArr : this.entityClasses.values()) { for (Class c : cArr) { config.addClass(c); } } this.configuration = config; return config.buildSessionFactory(); } finally { if (dataSource != null) { configTimeDataSourceHolder.set(null); } } } /** * Return the Configuration object used to build the SessionFactory. Allows * access to configuration metadata stored there (rarely needed). * * @throws IllegalStateException * if the Configuration object has not been initialized yet */ public final Configuration getConfiguration() { if (this.configuration == null) { throw new IllegalStateException("Configuration not initialized yet"); } return this.configuration; } public void destroy() throws HibernateException { DataSource dataSource = getDataSource(); if (dataSource != null) { configTimeDataSourceHolder.set(dataSource); } try { super.destroy(); } finally { if (dataSource != null) { configTimeDataSourceHolder.set(null); } } } } 类LocalDataSourceConnectionProvider的代码: java 代码 package wanged.core.persistent; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; import javax.sql.DataSource; import org.hibernate.HibernateException; import org.hibernate.connection.ConnectionProvider; import org.hibernate.util.JDBCExceptionReporter; public class LocalDataSourceConnectionProvider implements ConnectionProvider { private DataSource dataSource; private DataSource dataSourceToUse; public void configure(Properties props) throws HibernateException { this.dataSource = LocalSessionFactoryBean.getConfigTimeDataSource(); if (this.dataSource == null) { throw new HibernateException("No local DataSource found for configuration - " + "dataSource property must be set on LocalSessionFactoryBean"); } this.dataSourceToUse = getDataSourceToUse(this.dataSource); } protected DataSource getDataSourceToUse(DataSource originalDataSource) { return originalDataSource; } public DataSource getDataSource() { return dataSource; } public Connection getConnection() throws SQLException { try { return this.dataSourceToUse.getConnection(); } catch (SQLException ex) { JDBCExceptionReporter.logExceptions(ex); throw ex; } } public void closeConnection(Connection con) throws SQLException { try { con.close(); } catch (SQLException ex) { JDBCExceptionReporter.logExceptions(ex); throw ex; } } public void close() { } public boolean supportsAggressiveRelease() { return false; } }

相关推荐

    OSGi、Spring、Hibernate集成:动态注册实体类

    平台依赖度低的解决方案 ...测试环境:equinox3.5.2。其它用到的Bundle包括hibernate、hibernate-annotation、hsqldb、 spring、spring-osgi等。请到http://www.springsource.com/repository/下载。

    OSGI&spring;&hibernate;

    "表明我们将探讨三个关键的Java技术:OSGI(Open Services Gateway Initiative)、Spring框架以及Hibernate ORM(Object-Relational Mapping)工具。这三个技术在现代Java应用开发中占据着重要的地位。 OSGI是Java...

    OSGi+SpringMVC+Spring+Hibernate企业应用框架

    辛苦研究了一个多月,终于搭建成了一个可用于实际应用开发的OSGI-WEB框架,采用OSGi+SpringMVC+Spring+Hibernate+Virgo技术,鉴于此类技术架构甚少,特提供出来供大家参考。 使用步骤: 一、下载STS搭建Osgi开发...

    spring osgi相关资源

    3. 提供了丰富的库支持:包含了Spring框架、Hibernate、Tomcat等常见库,方便开发和部署。 四、spring-osgi-1.2.1-with-dependencies.zip 这个文件包含了Spring OSGi的核心库和其依赖,主要包含以下几个部分: 1. ...

    解决osgi spring 事务配置问题

    - 首先,我们需要确保引入了Spring OSGi相关的库,如`spring-osgi-core`, `spring-osgi-io`, `spring-osgi-extender`, `spring-osgi-aop`等。这些库提供了在OSGi环境中的Spring支持。 - 将Spring应用上下文作为...

    spring-osgi-1.2.0.zip

    Spring框架的1.2版本是较早的一个版本,它引入了许多核心特性,如依赖注入、AOP(面向切面编程)以及对其他技术的集成支持,比如JDBC和Hibernate。然而,Spring OSGi的1.2.0版本则是针对OSGi环境的特定版本,可能...

    Eclipse RCP与Spring OSGi技术详解与最佳实践

    《Eclipse RCP与Spring OSGi:技术详解与最佳实践》由资源的Eclipse专家亲自...实战篇(第13-15章)详细讲解了Eclipse RCP与Spring OSGi框架、Hibernate ORM框架、JPA规范、Maven工具的整合,以及它与Java的模块化设计

    SpringMVC+Spring+SpringDataJPA+Hibernate整合登录的效果

    这是整合SpringMVC+Spring+SpringDataJPA+Hibernate简单的实现登录的功能,用的是mysql数据库,这是一个web Project 如果你用的是JavaEE6那么你要注意bean-validator.jar和weld-osgi-bundle.jar与slf4j的jar包冲突。...

    基于virgo环境的OSGI+Maven的web开发代码下载(spring+hibernate+GWT)

    这个示例项目是一个综合性的学习资源,涵盖了现代Java Web开发的多个重要方面,包括OSGi的模块化、Maven的构建管理、Spring的依赖注入、Hibernate的ORM以及GWT的前端开发。对于想了解这些技术如何协同工作的开发者来...

    spring_jar包源码包

    6. **Spring ORM**:集成了各种ORM框架,如Hibernate、JPA等,帮助开发者在Spring应用中无缝地使用ORM技术。 源码包的存在,使我们有机会查看Spring的内部实现,了解其实现策略和设计模式。通过阅读源码,开发者...

    osgi_spring_hibernate_book:我关于将spring4和hibernate 4 java应用程序迁移到osgi环境的书

    将基于Hibernate 4和Spring 4的旧代码迁移到OSGi友好版本 当我听说OSGi Enterprise规范方法以及Spring团队使用其Spring DM服务器进入OSGi世界时,我真的以为OSGi很快将成为Java企业领域的标准。 但是随后,...

    osgi 资料 总结 实践

    OSGi(Open Services Gateway Initiative)是一种开放标准的Java模块化系统,它允许开发人员将应用程序分解为一组可独立更新和管理的...在与Spring和Hibernate等流行框架的集成中,OSGi同样展示了其广泛的应用价值。

    OSGI-in action

    《OSGI实战:结合Spring DM》 OSGI(Open Service Gateway Initiative)是一种模块化系统和服务平台,它允许软件以模块化的方式进行构建、部署和管理。OSGI在Java世界中扮演着重要的角色,因为它提供了动态性、可...

    osgi资料

    - 框架集成:讲解如何将OSGi与Spring、Hibernate等流行框架结合,构建复杂应用。 - 性能优化:提供关于提高OSGi应用性能的策略和技巧,如缓存管理和并发控制。 4. **Spring动态模型参考指南1.0.1.pdf**: 虽然...

    OSGi进阶源代码

    本资源集成了OSGi与SSH(Struts、Spring、Hibernate)三大主流框架的结合,使得基于OSGi的应用能够充分利用这些框架的功能,构建出更加灵活且强大的企业级应用。 **OSGi与SSH集成** 1. **Struts**:Struts是MVC...

    osgi进阶(中文)

    具体而言,它介绍了如何解决与Spring和Hibernate的集成问题,如何在Spring bean.xml文件中发布和引用OSGi服务,以及如何重构留言板列表模块。这些集成实践对于想要把现有应用迁移到OSGi平台的开发者来说至关重要。 ...

    OSGi原理与最佳实践(完整版)&OSGi_in_action

    3. **工具与框架**:介绍常用的OSGi开发工具和框架,如Apache Felix、Eclipse Equinox,以及如何与Spring、Hibernate等流行框架集成。 4. **案例研究**:提供真实世界中的OSGi应用案例,帮助读者理解OSGi在实际问题...

    virgo+hibernate+spring+springmvc项目

    这是一个基于Virgo服务器,结合Hibernate、Spring和SpringMVC的集成框架模板项目。这个项目旨在为开发者提供一个快速开发企业级应用的基础结构。下面将详细解释这些技术以及它们在项目中的作用。 **Virgo服务器** ...

Global site tag (gtag.js) - Google Analytics