As you may know, JBoss AS 7 is incredible fast and low-memory, and it comes with Hibernate 4.0.0 by default. Since AS 7.0.1, it is easy for you to support both Hibernate 3.x and Hibernate 4.0.0 under JBoss AS, thanks to the modular classpath here. If you want to run Hibernate 3.x under AS7 along with Hibernate 4, you should check out this blog entry and this documentation.
In RiftSaw 3 development, we are taking this opportunity to upgrading its JPA layer from JPA 1.0 to JPA 2.0, as JPA 2.0 is back compatible, it shouldn't be an big issue. However, we've implemented the ConnectionProvider and TransactionManagerLookup interfaces, since we've created the Datasource and TransactionManager on our own, we need to implement these two interfaces to ask Hibernate to use our own one.
In Hibernate 4.x, the whole package has been split into three categories, API/Impl/SPI. So the ConnectionProvider class has been re-packaged at: org.hibernate.service.jdbc.connections.spi.ConnectionProvider, if you see the internal implementation of this, you would also notice that it also implements the Configurable and Stoppable. The Configurable interface allows you to access to the Hibernate's properties, which is kept as Map. It is much like Spring's BeanNameAware interface, where you get to access to the BeanName. So below is the code snippet that I used for my ConnectionProvider.
public class DataSourceConnectionProvider implements ConnectionProvider, Configurable, Stoppable {
private Properties _props;
private boolean available = true;
public DataSourceConnectionProvider() {
}
@SuppressWarnings( {"unchecked"})
public void configure(Map properties) {
_props = new Properties();
_props.putAll(properties);
}
public Connection getConnection() throws SQLException {
if (!available) {
throw new HibernateException( "Provider is closed!" );
}
Connection c = HibernateUtil.getConnection(_props);
DbIsolation.setIsolationLevel(c);
return c;
}
public void closeConnection(Connection con) throws SQLException {
con.close();
}
public boolean supportsAggressiveRelease() {
return true;
}
public boolean isUnwrappableAs(Class unwrapType) {
return ConnectionProvider.class.equals(unwrapType) ||
DataSourceConnectionProvider.class.isAssignableFrom(unwrapType);
}
@SuppressWarnings( {"unchecked"})
public T unwrap(Class unwrapType) {
if (ConnectionProvider.class.equals(unwrapType) ||
DataSourceConnectionProvider.class.isAssignableFrom(unwrapType)) {
return (T) this;
} else {
throw new UnknownUnwrapTypeException( unwrapType );
}
}
public void stop() {
available = false;
}
}
For the TransactionManagerLookup interface, you would get WARN saying this API has been deprecated ( I believe it has been deprecated before 4.x, but I didn't get a chance to upgrade it until now), we should implement the org.hibernate.service.jta.platform.spi.JtaPlatform SPI instead, and set it to the 'hibernate.transaction.jta.platform' property. Instead of having a class that implement the JtaPlatform SPI directly, I've subclassed it from the AbstractJtaPlatform class, so that I've only needed to override two interfaces. Below is the code snippet for my custom JtaPlatform impl.
/**
*
* uses {@link HibernateUtil} to obtain the JTA {@link TransactionManager} object.
*
* @author Jeff Yu
*
*/
public class OdeJtaPlatform extends AbstractJtaPlatform {
private Properties properties = new Properties();
public void configure(Map configValues) {
super.configure(configValues);
properties.putAll(configValues);
}
@Override
protected TransactionManager locateTransactionManager() {
return HibernateUtil.getTransactionManager(properties);
}
@Override
protected UserTransaction locateUserTransaction() {
throw new UnsupportedOperationException("locateUserTransaction is not supported. Use the locateTransactionManager instead.");
}
}
And then when you create your entityManagerFactory, remember to pass the following properties in.
props.put(Environment.CONNECTION_PROVIDER, DataSourceConnectionProvider.class.getName());
props.put(Environment.JTA_PLATFORM, OdeJtaPlatform.class.getName());
Done, the custom ConnectionProvider and JtaPlatform implementation should be all set now.
Hope this will help you do the Hibernate upgrade at some point. Here, I'd like to thanks Strong Liu to send the helpful links to me on this upgrading. :-)
相关推荐
标题中提到的“Microsoft.Press.Exam.Ref.70-417.Upgrading Your Skills to MCSA”指的是为通过70-417考试做准备的一本参考书籍。这本书是微软官方出版社出版的,用于帮助读者更新或升级他们的技能以达到MCSA(微软...
根据提供的文档信息,本文将详细解析从FreeRTOS升级到SafeRTOS的主要差异,这些差异主要集中在记忆体配置、函式参数检查以及内部资料检查三个方面。 ### 一、记忆体配置 在从FreeRTOS升级到SafeRTOS的过程中,记忆...
MS Press - Upgrading Visual Basic 6.0 to VB.NET
Upgrading to Office SharePoint Server 2007.
升级至DB2版本9.7:详尽指南与关键知识点 在IT领域,数据库管理系统(DBMS)的更新换代是确保数据处理效率、安全性及功能性的关键步骤之一。IBM的DB2作为全球广泛使用的数据库软件之一,其每一次版本升级都备受关注。...
在这个版本中,性能得到了显著的提升,同时对开发者而言,语言的可用性和清晰度也有所增强。 ... 1. **改进的引擎:Zend Engine 3** ... - **标量类型声明**:允许开发者在函数参数和返回类型中指定必须是整型、浮点型、...
Upgrading Oracle Applications 11i to use JDK 1.3 (Doc ID 130091.1)
Oracle数据库是全球广泛使用的数据库管理系统之一,特别是在企业级应用中。11.2版本是Oracle数据库的一个重要里程碑,提供了许多增强的功能和优化。本篇文章将详细介绍Oracle数据库从11.2之前的版本升级到11.2的不同...
### 升级代码从 v6 到 .NET 遇到的问题 在升级代码从旧版本(v6)到 .NET 过程中遇到的问题主要包括编译错误和代码兼容性问题。本文将针对这些问题进行详细解析,并提供可能的解决方案。 #### 一、问题概述 ...
Upgrading to Oracle Workflow 2.6 with Oracle Applications 11i (Doc ID 144069.1)
Upgrading to J2SE 1.4.2 with Oracle Applications 11i (Doc ID 246105.1)
官方资料:[英文]白皮书:Upgrading to Oracle Database 12c (12.1.0.2)升级到ORACLE12c (12.1.0.2) Comparing Upgrade and Migration;Database Upgrade;Database Migration.;Multitenant Architecture;...
软件介绍 面向那些已经熟悉了PHP 4的开发者的,它提供了简明的关于PHP 4和PHP 5的区别及不同,以及最新PHP版本更新的细节和这些改变对 你的影响,同样覆盖了更多高级的特性,并通过短小的样例提供了手把手的经验,...
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET 英文原版 (微软出版社)
For 25 years, Upgrading and Repairing PCs has been the world’s #1 guide to PC hardware: The single source for reliable information on troubleshooting and fixing problems, adding hardware, optimizing ...
Upgrading to Java 6 on Oracle Application Server 10g Release 3 (10.1.3) (Doc ID 444462.1)
《升级到Visual Basic .NET》 随着技术的不断进步,编程语言也在持续演进,Visual Basic作为Microsoft开发的一款深受开发者喜爱的编程工具,也经历了从VB6到VB.NET的重大转变。这次升级不仅仅是版本的更新,更是一...
Upgrading and Repairing Servers Pages: 1128