Spring 4 Hibernate 4项目的JNDI数据源无法在AWS中运行

时间:2017-08-07 12:11:26

标签: spring hibernate amazon-web-services spring-mvc jndi

我试图连接我的Spring 4&使用JNDI Datasource在Amazon Beanstalk中托管的基于Hibernate 4的Web应用程序与Amazon RDS中的数据库。我使用SSH连接到我的EC2环境,并将以下内容放在/usr/share/tomcat8/conf/context.xml文件中;

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

在我的Hibernate配置文件中,我使用了以下内容;

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

我对JNDI在内部提供的内容感到困惑;

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

我能够成功连接到Amazon RDS数据库,同时使用我本地tomcat的context.xml文件中的上述配置本地保存我的代码,并进行以下查找;

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

但是当我在Amazon Beanstalk中托管我的代码时,同样的查找功能无效。它给了我以下例外情况;

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]

请!任何人,帮助我。

0 个答案:

没有答案