- 浏览: 541159 次
- 性别:
- 来自: 西安
文章分类
- 全部博客 (233)
- 设计模式 (1)
- WEBLOGIC (2)
- spring (2)
- struts (1)
- hibernate (8)
- SOA (11)
- j2ee (0)
- corba (0)
- linux (2)
- oracle (0)
- webService (17)
- javaMail (1)
- junit (3)
- java (45)
- 正则表达式 (7)
- java+xml (4)
- html (2)
- javaScript (6)
- ajax (1)
- mysql (0)
- eclipse (2)
- eclipse插件开发 (3)
- 软件工程 (5)
- ant (6)
- 开源包 (6)
- 学习网站 (7)
- Struts2 (18)
- JSP (1)
- 持续集成 (4)
- FreeMaker (6)
- 工作项目点点 (4)
- Maven2 (6)
- JQuery (1)
- 日志记录 (9)
- 工作流 (4)
- BPEL (1)
- extJs (1)
- 组件 (1)
- OSGI (10)
- ESB总线学习 (1)
- 错误解决 (2)
- JMX (3)
- 网络知识 (1)
最新评论
-
duanlongk:
这个问题解决了么?
java.lang.NoClassDefFoundError: org/apache/tuscany -
sotrip:
从上面结果来看,HelloWorld.class是由虚拟机的内 ...
如何实现自己的classloader -
zengsir2008:
哎,不太明白。。。。
OSGI DS 实例 -
ralfsumahe:
...
使用XFire开发Web Service客户端完整入门教程 -
liuqiao_0702:
...
osgi spring hibernate =
先来说说整合Hibernate的关键之处。其实用OSGi整合Hibernate很简单,但要通过Bundle方式做到可以扩展新的持久化层面的东西(比如添加新的表和操作)就比较费事了。因为Hibernate在初始化时根据注册的实体类创建SessionFactory,这样当有新的实体类添加进来时就要创建新的SessionFactory,这样系统中出现两个甚至多个SessionFatory会导致一系列的问题。显然整合Hibernate关键就是解决实体类注册与SessionFactory创建的问题。
我的具体思路如下。
首先将Hibernate单独多为一个Bundle(wanged_commons_hibernate)以便提供其他Bundle所需类包。
然后建立一个用于提供实体注册接口的Bundle(wanged_core_persistent_entity_register),代码如下:
- package wanged.core.persistent.entity;
- @SuppressWarnings("unchecked")
- public interface EntityRegister {
- /**
- * 注册Hibernate的实体Class
- * @return
- */
- Class[] register();
- }
建一个用来初始化SessionFactory和事务管理的Bundle(wanged_core_persistent),由于使用Spring提供的LocalSessionFactoryBean会有问题,所以我单独写了两个类。
类LocalSessionFactoryBean的代码:
- 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的代码:
- package wanged.core.persistent;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.util.Properties;
- import javax.sql.DataSource;
- 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保存时经常保存不住,需要重写,所以我尽可能的将其划分开来写。
- 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中:
- <!-- 本地调试使用连接池-->
- <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中:
- <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" />
评论
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; } }
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 =
2009-04-23 17:27 2488接上文,为了测试已经搭好的架子,创建了一个注册实体的Bundl ... -
OpenCore: OSGi上部署Hibernate的四种方式
2009-04-23 16:33 1364本文讨论四种解决方案:1.模型对象(Domain Object ... -
the type org.springframework.dao.support.daosuppor
2009-04-13 15:02 8371I am using only the 3rd party b ... -
[Eclipse] Access restriction: Class is not accessi
2009-04-13 09:47 2856[Eclipse] Access r ... -
Service Event Listener Bundle
2009-04-09 10:52 1511This example creates a simple b ... -
解决OSGI环境第三方包的NoClassDefFoundError
2009-04-07 17:48 2584总结了一下:1、把第三方包的class文件改造为一个bundl ... -
OSGI StartLevel explained and using 启动级别机制和使用
2009-03-17 14:52 4078OSGI StartLevel explained and u ... -
收藏一个OSGI网站
2009-03-04 16:28 1119http://www.blogjava.net/Phranco ... -
OSGI DS 实例
2009-02-16 09:16 3356一,开发生产者: 工程名:org.vwpolo.os ...
相关推荐
平台依赖度低的解决方案 ...测试环境:equinox3.5.2。其它用到的Bundle包括hibernate、hibernate-annotation、hsqldb、 spring、spring-osgi等。请到http://www.springsource.com/repository/下载。
"表明我们将探讨三个关键的Java技术:OSGI(Open Services Gateway Initiative)、Spring框架以及Hibernate ORM(Object-Relational Mapping)工具。这三个技术在现代Java应用开发中占据着重要的地位。 OSGI是Java...
辛苦研究了一个多月,终于搭建成了一个可用于实际应用开发的OSGI-WEB框架,采用OSGi+SpringMVC+Spring+Hibernate+Virgo技术,鉴于此类技术架构甚少,特提供出来供大家参考。 使用步骤: 一、下载STS搭建Osgi开发...
3. 提供了丰富的库支持:包含了Spring框架、Hibernate、Tomcat等常见库,方便开发和部署。 四、spring-osgi-1.2.1-with-dependencies.zip 这个文件包含了Spring OSGi的核心库和其依赖,主要包含以下几个部分: 1. ...
- 首先,我们需要确保引入了Spring OSGi相关的库,如`spring-osgi-core`, `spring-osgi-io`, `spring-osgi-extender`, `spring-osgi-aop`等。这些库提供了在OSGi环境中的Spring支持。 - 将Spring应用上下文作为...
Spring框架的1.2版本是较早的一个版本,它引入了许多核心特性,如依赖注入、AOP(面向切面编程)以及对其他技术的集成支持,比如JDBC和Hibernate。然而,Spring OSGi的1.2.0版本则是针对OSGi环境的特定版本,可能...
《Eclipse RCP与Spring OSGi:技术详解与最佳实践》由资源的Eclipse专家亲自...实战篇(第13-15章)详细讲解了Eclipse RCP与Spring OSGi框架、Hibernate ORM框架、JPA规范、Maven工具的整合,以及它与Java的模块化设计
这是整合SpringMVC+Spring+SpringDataJPA+Hibernate简单的实现登录的功能,用的是mysql数据库,这是一个web Project 如果你用的是JavaEE6那么你要注意bean-validator.jar和weld-osgi-bundle.jar与slf4j的jar包冲突。...
这个示例项目是一个综合性的学习资源,涵盖了现代Java Web开发的多个重要方面,包括OSGi的模块化、Maven的构建管理、Spring的依赖注入、Hibernate的ORM以及GWT的前端开发。对于想了解这些技术如何协同工作的开发者来...
6. **Spring ORM**:集成了各种ORM框架,如Hibernate、JPA等,帮助开发者在Spring应用中无缝地使用ORM技术。 源码包的存在,使我们有机会查看Spring的内部实现,了解其实现策略和设计模式。通过阅读源码,开发者...
将基于Hibernate 4和Spring 4的旧代码迁移到OSGi友好版本 当我听说OSGi Enterprise规范方法以及Spring团队使用其Spring DM服务器进入OSGi世界时,我真的以为OSGi很快将成为Java企业领域的标准。 但是随后,...
OSGi(Open Services Gateway Initiative)是一种开放标准的Java模块化系统,它允许开发人员将应用程序分解为一组可独立更新和管理的...在与Spring和Hibernate等流行框架的集成中,OSGi同样展示了其广泛的应用价值。
《OSGI实战:结合Spring DM》 OSGI(Open Service Gateway Initiative)是一种模块化系统和服务平台,它允许软件以模块化的方式进行构建、部署和管理。OSGI在Java世界中扮演着重要的角色,因为它提供了动态性、可...
- 框架集成:讲解如何将OSGi与Spring、Hibernate等流行框架结合,构建复杂应用。 - 性能优化:提供关于提高OSGi应用性能的策略和技巧,如缓存管理和并发控制。 4. **Spring动态模型参考指南1.0.1.pdf**: 虽然...
本资源集成了OSGi与SSH(Struts、Spring、Hibernate)三大主流框架的结合,使得基于OSGi的应用能够充分利用这些框架的功能,构建出更加灵活且强大的企业级应用。 **OSGi与SSH集成** 1. **Struts**:Struts是MVC...
具体而言,它介绍了如何解决与Spring和Hibernate的集成问题,如何在Spring bean.xml文件中发布和引用OSGi服务,以及如何重构留言板列表模块。这些集成实践对于想要把现有应用迁移到OSGi平台的开发者来说至关重要。 ...
3. **工具与框架**:介绍常用的OSGi开发工具和框架,如Apache Felix、Eclipse Equinox,以及如何与Spring、Hibernate等流行框架集成。 4. **案例研究**:提供真实世界中的OSGi应用案例,帮助读者理解OSGi在实际问题...
这是一个基于Virgo服务器,结合Hibernate、Spring和SpringMVC的集成框架模板项目。这个项目旨在为开发者提供一个快速开发企业级应用的基础结构。下面将详细解释这些技术以及它们在项目中的作用。 **Virgo服务器** ...