Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
dda3197c
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
dda3197c
编写于
7月 06, 2012
作者:
J
Juergen Hoeller
提交者:
unknown
7月 06, 2012
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added "jtaTransactionManager" property to Hibernate 4 LocalSessionFactoryBean/Builder
Issue: SPR-9480
上级
9aa43fce
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
152 addition
and
3 deletion
+152
-3
spring-orm/src/main/java/org/springframework/orm/hibernate4/ConfigurableJtaPlatform.java
...ringframework/orm/hibernate4/ConfigurableJtaPlatform.java
+74
-0
spring-orm/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBean.java
...ringframework/orm/hibernate4/LocalSessionFactoryBean.java
+22
-0
spring-orm/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.java
...gframework/orm/hibernate4/LocalSessionFactoryBuilder.java
+56
-3
未找到文件。
spring-orm/src/main/java/org/springframework/orm/hibernate4/ConfigurableJtaPlatform.java
0 → 100644
浏览文件 @
dda3197c
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.orm.hibernate4
;
import
javax.transaction.TransactionManager
;
import
javax.transaction.UserTransaction
;
import
org.hibernate.service.jta.platform.internal.AbstractJtaPlatform
;
import
org.springframework.transaction.jta.UserTransactionAdapter
;
import
org.springframework.util.Assert
;
/**
* Implementation of Hibernate 4's {@link org.hibernate.service.jta.platform.spi.JtaPlatform}
* SPI, exposing passed-in {@link TransactionManager} and {@link UserTransaction} references.
*
* @author Juergen Hoeller
* @since 3.1.2
*/
class
ConfigurableJtaPlatform
extends
AbstractJtaPlatform
{
private
final
TransactionManager
transactionManager
;
private
final
UserTransaction
userTransaction
;
/**
* Create a new ConfigurableJtaPlatform instance with the given
* JTA TransactionManager and optionally a given UserTransaction.
* @param tm the JTA TransactionManager reference (required)
* @param ut the JTA UserTransaction reference (optional)
*/
public
ConfigurableJtaPlatform
(
TransactionManager
tm
,
UserTransaction
ut
)
{
Assert
.
notNull
(
tm
,
"TransactionManager reference must not be null"
);
this
.
transactionManager
=
tm
;
this
.
userTransaction
=
(
ut
!=
null
?
ut
:
new
UserTransactionAdapter
(
tm
));
}
@Override
protected
TransactionManager
locateTransactionManager
()
{
return
this
.
transactionManager
;
}
@Override
protected
UserTransaction
locateUserTransaction
()
{
return
this
.
userTransaction
;
}
@Override
protected
boolean
canCacheTransactionManager
()
{
return
true
;
}
@Override
protected
boolean
canCacheUserTransaction
()
{
return
true
;
}
}
spring-orm/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBean.java
浏览文件 @
dda3197c
...
...
@@ -48,6 +48,12 @@ import org.springframework.core.io.support.ResourcePatternUtils;
* However, in practice, it is closer to <code>AnnotationSessionFactoryBean</code> since
* its core purpose is to bootstrap a <code>SessionFactory</code> from annotation scanning.
*
* <p><b>NOTE:</b> To set up Hibernate 4 for Spring-driven JTA transactions, make
* sure to either specify the {@link #setJtaTransactionManager "jtaTransactionManager"}
* bean property or to set the "hibernate.transaction.factory_class" property to
* {@link org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory}.
* Otherwise, Hibernate's smart flushing mechanism won't work properly.
*
* @author Juergen Hoeller
* @since 3.1
* @see #setDataSource
...
...
@@ -82,6 +88,8 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
private
String
[]
packagesToScan
;
private
Object
jtaTransactionManager
;
private
ResourcePatternResolver
resourcePatternResolver
=
new
PathMatchingResourcePatternResolver
();
private
Configuration
configuration
;
...
...
@@ -250,6 +258,16 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
this
.
packagesToScan
=
packagesToScan
;
}
/**
* Set the Spring {@link org.springframework.transaction.jta.JtaTransactionManager}
* or the JTA {@link javax.transaction.TransactionManager} to be used with Hibernate,
* if any.
* @see LocalSessionFactoryBuilder#setJtaTransactionManager
*/
public
void
setJtaTransactionManager
(
Object
jtaTransactionManager
)
{
this
.
jtaTransactionManager
=
jtaTransactionManager
;
}
public
void
setResourceLoader
(
ResourceLoader
resourceLoader
)
{
this
.
resourcePatternResolver
=
ResourcePatternUtils
.
getResourcePatternResolver
(
resourceLoader
);
}
...
...
@@ -330,6 +348,10 @@ public class LocalSessionFactoryBean extends HibernateExceptionTranslator
sfb
.
scanPackages
(
this
.
packagesToScan
);
}
if
(
this
.
jtaTransactionManager
!=
null
)
{
sfb
.
setJtaTransactionManager
(
this
.
jtaTransactionManager
);
}
// Build SessionFactory instance.
this
.
configuration
=
sfb
;
this
.
sessionFactory
=
buildSessionFactory
(
sfb
);
...
...
spring-orm/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.java
浏览文件 @
dda3197c
/*
* Copyright 2002-201
1
the original author or authors.
* Copyright 2002-201
2
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -22,12 +22,16 @@ import javax.persistence.Embeddable;
import
javax.persistence.Entity
;
import
javax.persistence.MappedSuperclass
;
import
javax.sql.DataSource
;
import
javax.transaction.TransactionManager
;
import
org.hibernate.HibernateException
;
import
org.hibernate.MappingException
;
import
org.hibernate.SessionFactory
;
import
org.hibernate.cfg.AvailableSettings
;
import
org.hibernate.cfg.Configuration
;
import
org.hibernate.cfg.Environment
;
import
org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory
;
import
org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.ResourceLoader
;
...
...
@@ -39,6 +43,8 @@ import org.springframework.core.type.classreading.MetadataReader;
import
org.springframework.core.type.classreading.MetadataReaderFactory
;
import
org.springframework.core.type.filter.AnnotationTypeFilter
;
import
org.springframework.core.type.filter.TypeFilter
;
import
org.springframework.transaction.jta.JtaTransactionManager
;
import
org.springframework.util.Assert
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.util.ReflectionUtils
;
...
...
@@ -50,6 +56,11 @@ import org.springframework.util.ReflectionUtils;
* <p>This is designed for programmatic use, e.g. in {@code @Bean} factory methods.
* Consider using {@link LocalSessionFactoryBean} for XML bean definition files.
*
* <p><b>NOTE:</b> To set up Hibernate 4 for Spring-driven JTA transactions, make
* sure to either use the {@link #setJtaTransactionManager} method or to set the
* "hibernate.transaction.factory_class" property to {@link CMTTransactionFactory}.
* Otherwise, Hibernate's smart flushing mechanism won't work properly.
*
* @author Juergen Hoeller
* @since 3.1
* @see LocalSessionFactoryBean
...
...
@@ -97,18 +108,60 @@ public class LocalSessionFactoryBuilder extends Configuration {
* Create a new LocalSessionFactoryBuilder for the given DataSource.
* @param dataSource the JDBC DataSource that the resulting Hibernate SessionFactory should be using
* (may be <code>null</code>)
* @param
class
Loader the ResourceLoader to load application classes from
* @param
resource
Loader the ResourceLoader to load application classes from
*/
public
LocalSessionFactoryBuilder
(
DataSource
dataSource
,
ResourceLoader
resourceLoader
)
{
getProperties
().
put
(
Environment
.
CURRENT_SESSION_CONTEXT_CLASS
,
SpringSessionContext
.
class
.
getName
());
if
(
dataSource
!=
null
)
{
getProperties
().
put
(
Environment
.
DATASOURCE
,
dataSource
);
}
getProperties
().
put
(
"hibernate.classLoader.application"
,
resourceLoader
.
getClassLoader
());
getProperties
().
put
(
AvailableSettings
.
APP_CLASSLOADER
,
resourceLoader
.
getClassLoader
());
this
.
resourcePatternResolver
=
ResourcePatternUtils
.
getResourcePatternResolver
(
resourceLoader
);
}
/**
* Set the Spring {@link JtaTransactionManager} or the JTA {@link TransactionManager}
* to be used with Hibernate, if any. Allows for using a Spring-managed transaction
* manager for Hibernate 4's session and cache synchronization, with the
* "hibernate.transaction.jta.platform" automatically set to it. Also sets
* "hibernate.transaction.factory_class" to {@link CMTTransactionFactory},
* instructing Hibernate to interact with externally managed transactions.
* <p>A passed-in Spring {@link JtaTransactionManager} needs to contain a JTA
* {@link TransactionManager} reference to be usable here, except for the WebSphere
* case where we'll automatically set {@link WebSphereExtendedJtaPlatform} accordingly.
* <p>Note: If this is set, the Hibernate settings should not contain a JTA platform
* setting to avoid meaningless double configuration.
*/
public
LocalSessionFactoryBuilder
setJtaTransactionManager
(
Object
jtaTransactionManager
)
{
Assert
.
notNull
(
jtaTransactionManager
,
"Transaction manager reference must not be null"
);
if
(
jtaTransactionManager
instanceof
JtaTransactionManager
)
{
boolean
webspherePresent
=
ClassUtils
.
isPresent
(
"com.ibm.wsspi.uow.UOWManager"
,
getClass
().
getClassLoader
());
if
(
webspherePresent
)
{
getProperties
().
put
(
AvailableSettings
.
JTA_PLATFORM
,
new
WebSphereExtendedJtaPlatform
());
}
else
{
JtaTransactionManager
jtaTm
=
(
JtaTransactionManager
)
jtaTransactionManager
;
if
(
jtaTm
.
getTransactionManager
()
==
null
)
{
throw
new
IllegalArgumentException
(
"Can only apply JtaTransactionManager which has a TransactionManager reference set"
);
}
getProperties
().
put
(
AvailableSettings
.
JTA_PLATFORM
,
new
ConfigurableJtaPlatform
(
jtaTm
.
getTransactionManager
(),
jtaTm
.
getUserTransaction
()));
}
}
else
if
(
jtaTransactionManager
instanceof
TransactionManager
)
{
getProperties
().
put
(
AvailableSettings
.
JTA_PLATFORM
,
new
ConfigurableJtaPlatform
((
TransactionManager
)
jtaTransactionManager
,
null
));
}
else
{
throw
new
IllegalArgumentException
(
"Unknown transaction manager type: "
+
jtaTransactionManager
.
getClass
().
getName
());
}
getProperties
().
put
(
AvailableSettings
.
TRANSACTION_STRATEGY
,
new
CMTTransactionFactory
());
return
this
;
}
/**
* Add the given annotated classes in a batch.
* @see #addAnnotatedClass
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录