引起原因:HibernateException:如果在org.hibernate.context.internal.ThreadLocalSessionContext上没有活动事务,createCriteria无效

时间:2018-11-15 22:51:00

标签: java spring hibernate spring-mvc

  

由以下原因引起:org.hibernate.HibernateException:createCriteria不是   有效,无有效交易   org.hibernate.context.internal.ThreadLocalSessionContext $ TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)                         com.sun.proxy。$ Proxy63.createCriteria(未知来源)

休眠配置

package com.cm;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@PropertySource("classpath:config.properties")
@ComponentScan(basePackages = "com.cm")
public class HibernateConfiguration {


    @Value("${db.username}")
    private String userName;
    @Value("${db.password}")
    private String password;
    @Value("${db.jdbcurl}")
    private String jdbcurl;

    /*@Value("${hibernate.dialect}")
    private String dialect;*/
    /*@Value("${hibernate.show_sql}")
    private String show_sql;
    @Value("${hibernate.format_sql}")
    private String format_sql;
*/
    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "com.cm.models" });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
    }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl(jdbcurl);
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        return dataSource;
    }

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        properties.put("hibernate.show_sql", true);
        properties.put("hibernate.format_sql", true);
        return properties;
    }

    @Bean
    @Autowired
    public HibernateTemplate getHibernateTemplate(SessionFactory sessionFactory)
    {
        HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory);
        return hibernateTemplate;
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory s) {
        HibernateTransactionManager txManager = new HibernateTransactionManager();
        txManager.setSessionFactory(s);
        return txManager;
    }

}

@Service
public class TransactionService {


    private TransactionDao transactionDao;  
    private TransactionDetailsDao transactionDetailDao;

    @Autowired
    public TransactionService(TransactionDao transactionDao, TransactionDetailsDao transactionDetailDao){
        this.transactionDao = transactionDao;
        this.transactionDetailDao = transactionDetailDao;
    }
transactionDao.selectFromTransaction(bookRoomsRequest.getConfirmationNo(), bookRoomsRequest.getBookingStatus());
}

**DAO**
  

如果我正在编写@Transection,那么我会遇到bean创建问题   在应用程序启动时,我也在服务层上应用了但相同   问题。

@Repository 
public class TransactionDaoImpl extends AbstractDao implements TransactionDao {

@SuppressWarnings("unchecked")
    @Override
    public boolean selectFromTransaction(String orderId, String type) throws SQLException {
        /*
         * String SQL_QUERY = "select * from transaction t where t.type = '" + type +
         * "' and  t.order_id='" + orderId +"';";
         */
        List<Transaction> list = new ArrayList<>();
        boolean response = false;

        try {
            Criteria criteria = getSession().createCriteria(Transaction.class);
            Criterion Type = (Restrictions.eq("type", type));
            Criterion Order_id = (Restrictions.eq("order_id", orderId));
            criteria.add(Restrictions.and(Type, Order_id));
            list = criteria.list();

            if (!CollectionUtils.isEmpty(list)) {
                response = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return response;
    }
}

如果我要从ThreadLocalSessionContext中删除hibernate configuration,那么我将遇到错误。

  

org.hibernate.HibernateException:无法获取   当前线程的事务同步会话   org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)

我正在妥善管理交易,并且尝试了所有可能的解决方案(Spring Hibernate - Could not obtain transaction-synchronized Session for current thread),但仍然没有运气,请帮助获得此问题的确切解决方案。

**On Putting @Transectional  on on selectFromTransaction method i am getting below erro Stacktrace at application statup.**
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'retryServiceImpl' defined in file [/home/saurabhsharma/Desktop/fresh_proxycm/hotels-proxy-cm/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/cm-proxy-api/WEB-INF/classes/com/paytm/cm/services/impl/RetryServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 1: No qualifying bean of type [com.paytm.cm.dao.impl.TransactionDaoImpl] found for dependency [com.paytm.cm.dao.impl.TransactionDaoImpl]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.paytm.cm.dao.impl.TransactionDaoImpl] found for dependency [com.paytm.cm.dao.impl.TransactionDaoImpl]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:187) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1213) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1053) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:834) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    ... 24 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.paytm.cm.dao.impl.TransactionDaoImpl] found for dependency [com.paytm.cm.dao.impl.TransactionDaoImpl]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1403) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1056) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:834) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
    ... 39 common frames omitted
Nov 16, 2018 11:40:53 AM org.apache.c

0 个答案:

没有答案