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

  • SEARCH
  • Home
  • 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 7163195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:51:40+00:00 2026-05-28T13:51:40+00:00

I’m using Spring and Hibernate with annotation-driven transactions. When running my application I’ve received

  • 0

I’m using Spring and Hibernate with annotation-driven transactions.
When running my application I’ve received an exception “createCriteria is not valid without active transaction”. According to this Spring/Hibernate Exception: createCriteria is not valid without active transaction the solution is to remove the line <property name="current_session_context_class">thread</property> from sessionFactory configuration.

However, I also need to do some transactional work in my @PostConstruct method (initialization from DB). @PostConstruct methods can’t be transactional, so I open a manual transaction – but this does not work when I remove the the above line (getting an exception org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here). According to several sources the solution to this is to add <property name="current_session_context_class">thread</property> to configuration…

This is my code (I’m aware it’s not too nice and clean – I’ve been fiddling with it to understand what the problems were):

`@Transactional
public class TaskControllerImpl implements TaskController {

@Autowired
TaskDAO taskDAO;

@Autowired
MethodDAO methodDAO;

@Autowired
SessionFactory sessionFactory;

ExecutorService threadPool = Executors.newCachedThreadPool();


/**
 * Map of method name to TaskExecutor for all defined methods
 */
private Map<String, TaskExecutor> executorsByMethod;

final Logger logger = LoggerFactory.getLogger(TaskControllerImpl.class);

/**
 * Initializes the mapping of method name to TaskExecutor
 */
@PostConstruct
public void init() {
    // @Transactional has no effect in @Postconstruct methods so must do this manually
    Transaction t = sessionFactory.getCurrentSession().beginTransaction();
    executorsByMethod = new HashMap<String, TaskExecutor>();
    List<Method> methods = methodDAO.findAll();
    for (Method method : methods) {         
        if (method.getExecutorClassName() != null) {
            try {
                TaskExecutor executor = createTaskExecutor(method);
                executorsByMethod.put(method.getName(), executor);
            } catch (Throwable e) {
                logger.error("Coud not create/instantiate executor " + method.getExecutorClassName());
                e.printStackTrace();
            }
        }       
    }

    t.commit();
}

@Override
public void run() {
    Collection<Task> tasksToExecute = fetchTasksToExecute();
    for (Task task : tasksToExecute) {
        String method = task.getMethod().getName();
        executorsByMethod.get(method).addTask(task);
    }
}

/**
 * Fetches all tasks which need to be executed at the current time
 * 
 * @return
 */
private Collection<Task> fetchTasksToExecute() {
    try {
        Search search = new Search();
        search.addFilterLessThan("actionDate", DateUtil.now());
        search.addFilterEqual("status", TaskStatus.PENDING.getCode());
        search.addSort("actionDate", false);
        return taskDAO.search(search);
    } catch (Throwable e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}

`

Configuration:

`

<!-- Configure a JDBC datasource for Hibernate to connect with -->
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="${connection.url}" />
    <property name="username" value="${connection.username}" />
    <property name="password" value="${connection.password}" />
</bean>

<!-- Configure a Hibernate SessionFactory -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.grroo.model" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <!-- prop key="hibernate.current_session_context_class">thread</prop-->
            <prop key="hibernate.connection.zeroDateTimeBehavior">convertToNull</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven/> 

`

So I’m in a bit of a catch-22 here on how to make both init() and run() work. Any ideas?

  • 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-28T13:51:41+00:00Added an answer on May 28, 2026 at 1:51 pm

    You just need to extract your transactional method from the postconstruct, and then call it. For example:

    @PostConstruct
    public void postConstruct(){
        init();
    }
    
    @Transactional
    public void init(){
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need a function that will clean a strings' special characters. I do NOT

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.