Reputation: 65
I'm having this error while trying to connect to mySQL database:
No object bound to name java:comp/env/jdbc/mySql
This is my configuration class:
@Configuration
@EnableTransactionManagement
@ComponentScan({ "org.onmyown.config" })
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfiguration {
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "org.onmyown" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DataSource dataSource = null;
try {
Context initialContext = new InitialContext();
Context environmentContext = (Context)initialContext.lookup("java:comp/env");
dataSource = (DataSource) environmentContext.lookup("jdbc/mySql");
} catch (NamingException e) {
e.printStackTrace();
}
return dataSource;
}
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"));
properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
return properties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
}
I can ping from GlassFish just fine. I put all the properties I need (I think). My friend did this configuration with Derby database and it works. Is there any difference?
@edit
I 'fixed' something. I added web.xml, everything is here:
https://github.com/afterlook/SpringMVC
The problem now is that the application doesn't really care that i put mySql as a data source. It searches for DerbyPool which is one of the defaults in GlassFish. Any idea why?
Upvotes: 1
Views: 686
Reputation: 9526
The problem you posted originaly tries to use a datasource that is looked up using JNDI with the key comp/env/jdbc/mySql and this is not configured.
If you want to use a connection pool of Glasfish, you have to configure Glasfish so it creates that pool and publishes it as a JNDI component with the key comp/env/jdbc/mySql mysql glasfish shows how this is done.
With your fix you commented the JNDI code out, instead you set the connection url from a property.
db.url=jdbc:mysql://localhost:3306/app
without additional configuration, this seems to point to the Glasfish default connection pool using the derby db.
If you want to use the Glasfish connection pool :
Revert your fix that commented out the JNDI lookup. Your code should look like this :
@Bean
public DataSource dataSource() {
DataSource dataSource = null;
try {
Context initialContext = new InitialContext();
Context environmentContext = (Context)initialContext.lookup("java:comp/env");
dataSource = (DataSource) environmentContext.lookup("jdbc/mySql");
} catch (NamingException e) {
e.printStackTrace();
}*/
return dataSource;
}
Upvotes: 1