JNDI Datasource for Spring 4 Hibernate 4 project not working in AWS

旧时模样 提交于 2019-12-11 08:21:59

问题


I'm trying to connect my Spring 4 & Hibernate 4 based web application hosted in Amazon Beanstalk with the database in Amazon RDS using JNDI Datasource. I connected to my EC2 environment using SSH and placed the following in /usr/share/tomcat8/conf/context.xml file;

<Resource 
    auth="Container" 
    driverClassName="com.mysql.jdbc.Driver" 
    name="jdbc/TestDatabase" 
    password="abc123" 
    type="javax.sql.DataSource" 
    url="xyz.com:3306/test_jndi" 
    username="abc123"/>

And in my Hibernate configuration file I used the below;

@Bean
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    sessionFactory.setPackagesToScan(new String[] { "com.example.spring.model" });
    sessionFactory.setHibernateProperties(hibernateProperties());
    return sessionFactory;
}

@Bean
public DataSource dataSource() {
    try {
        return (DataSource) new 
        JndiTemplate().lookup("don't know what to provide");
    } catch (NamingException e) {
        e.printStackTrace();
        return null;
    }
}

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

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

I'm confused on what JNDI look up to provide inside;

JndiTemplate().lookup("don't know what to provide");

I was able to successfully connect to Amazon RDS database while keeping my code locally with the above configuration in context.xml file of my local tomcat and with the following look up;

JndiTemplate().lookup("java:comp/env/jdbc/TestDatabase");

But the same look up is not working when I host my code in Amazon Beanstalk. It's giving me the following exception;

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'transactionManager' defined in class path resource [com/example/spring/config/HibernateConfiguration.class]: 
Invocation of init method failed; nested exception is org.hibernate.service.UnknownUnwrapTypeException: 
Cannot unwrap to requested type [javax.sql.DataSource]

Please! anyone, help me with this.

来源:https://stackoverflow.com/questions/45546603/jndi-datasource-for-spring-4-hibernate-4-project-not-working-in-aws

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!