Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6190713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:38:16+00:00 2026-05-24T02:38:16+00:00

I am developing a part of a software which has two modules(mail and web-service).But

  • 0

I am developing a part of a software which has two modules(mail and web-service).But I have some confusion in my code which I think that I am doing in wrong way/ in bad way / in difficult ways. Could somebody help me to overcome these? Th problem is hibernate is closing the session.

Here is my pom structure

 -- root
    -- mail
    -- web-service

Point to be noted that my spring bootstrapping,appContext.xml,persistence context are all managed my web-service module and I am using mail module as dependency jar in my web-service module.

My,appContext.xml is like this,

<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>

I wanted to make mail module as independent as possible. Also the mail configuration comes from database table:

table : "custom":(smtp,global_sender)

So I made my own JavaMailSenderImpl where I override the getHost method like this.I have made this as spring @Component . My confusions are in comments inside the code.

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;
}

}

In the web service module I have a service NotificationService where I am injecting spring mail service.

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*/ }

Why my EntityManager is getting closed? Is this Container managed ? what caused the closed sesesion?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:38:16+00:00Added an answer on May 24, 2026 at 2:38 am

    Don’t use PersistenceContextType.EXTENDED (unless you know very well what you are doing). See here for a detailed explanation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing client part of web application. So i have some controls like
I am developing silverlight web part for sharepoint 2010. I have an xml file
I am developing a CAD software in which for my graphics part i am
I'm developing a SharePoint 2010 Web Part (in C#) which needs to pull user
I'm developing some code to present currency symbols as part of a label on
I am developing a small part of a PHP application with some python code.
I am developing silverlight web part. I have a one List Entry log. In
I take part in developing a Java project, which uses some C++ components, thus
I am developing silverlight web part for sharepoint 2010. In my project I have
I am developing a web part in C# which is reading the contents of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.