gpt4 book ai didi

使用实体管理器关闭 hibernate session

转载 作者:行者123 更新时间:2023-12-03 23:32:50 25 4
gpt4 key购买 nike

我正在开发一个软件的一部分,它有两个模块(邮件和网络服务)。但是我的代码有些困惑,我认为我的做法是错误的/不好的/困难的。有人可以帮我克服这些吗?问题是 hibernate 正在关闭 session 。

这是我的pom结构

 -- root
-- mail
-- web-service

需要注意的是,我的 spring bootstrapping、appContext.xml、persistence context 都由我的 web 服务模块管理,并且我在我的 web 服务模块中使用邮件模块作为依赖 jar。

我的,appContext.xml 是这样的,
<context:component-scan base-package="com.it.ese.orbitws,com.it.ese.orbitmail" />

<context:annotation-config />
<tx:annotation-driven />


<!-- enables interpretation of the @PersistenceUnit/@PersistenceContext annotations providing convenient
access to EntityManagerFactory/EntityManager -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>


<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://localhost:1433;database=ORBIT_RC" />
<property name="username" value="sa" />
<property name="password" value="sa" />
</bean>

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect" />
<property name="showSql" value="true" />
</bean>
</property>
<property name="dataSource" ref="dataSource" />
</bean>


<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
<qualifier value="tx"/>
</bean>

我想让邮件模块尽可能独立。邮件配置也来自数据库表:
table : "custom":(smtp,global_sender)

所以我自己做了 JavaMailSenderImpl我像这样覆盖 getHost 方法。我把它作为 Spring @Component .我的困惑在于代码中的注释。
package com.it.ese.mail
@Component
public class OrbitJavaMailSenderImpl extends JavaMailSenderImpl{


private EntityManager entityManager;
private String smtp=null;

private OrbitJavaMailSenderImpl() {
}

// I have used PersistenceContextType.EXTENDED because without this,it says
// Hibernate Session is closed.But using EXTENDED is a bad thing?or it's ok?

@PersistenceContext (type= PersistenceContextType.EXTENDED)
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}

//There is no Bean for CUStom TABLE so far in persistence.xml.Should I do that too?

@Transactional(readOnly=true)
public String getSMTPAddress() {
String host="mail.esolutionss-europe.com";
try{
host= this.entityManager.createNativeQuery("SELECT smtp FROM CUSTOM")
.getSingleResult().toString();
}catch(Exception e){
System.out.println("error getting host:"+e.toString());

}

return host;
}

}

在 Web 服务模块中,我有一个服务 NotificationService我在哪里注入(inject) spring 邮件服务。
package com.it.ese.ws.service 
public class NotificationService implements INotificationService {

private EntityManager entityManager;
private String sender=null;

final static Logger logger = Logger.getLogger(NotificationService.class);

@PersistenceContext (type= PersistenceContextType.EXTENDED)
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}


//Also here if I don't use EXTENTED persistence context,it gives me error as hibernate session is closed.

@Transactional(readOnly=true)
public String getGlobalSender() {
String sender="a1@xxx.com";
try{
sender= entityManager.createNativeQuery("SELECT Global_email FROM CUSTOM")
.getSingleResult().toString();
}catch(Exception e){

}
return sender;
} /*other methods*/ }

为什么我的 EntityManager 正在关闭?这个容器是托管的吗?是什么导致了闭会?

最佳答案

不要使用 PersistenceContextType.EXTENDED (除非你非常清楚自己在做什么)。 See here详细说明。

关于使用实体管理器关闭 hibernate session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6820905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com