I get an error whenever I try to deploy my Spring war application to Glassfish server ( with Glassfish 4 and 5 ):
However, everything is okay if I run the application straight from the Eclipse. My project is Maven project, I just right click the projeck and the choose Run As -> Run On server
Here's my glassfish-resources.xml, its located in the WEB-INF folder and its included in the deployment ( I've verified it )
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="mysql_pos_rootPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
<property name="serverName" value="localhost"/>
<property name="portNumber" value="3306"/>
<property name="databaseName" value="pos"/>
<property name="User" value="root"/>
<property name="Password" value="root"/>
<property name="URL" value="jdbc:mysql://localhost:3306/pos?zeroDateTimeBehavior=convertToNull"/>
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
</jdbc-connection-pool>
<jdbc-resource enabled="true" jndi-name="java:app/pos" object-type="user" pool-name="mysql_pos_rootPool"/>
</resources>
The beans in my RootContextConfiguration class:
@Bean
public DataSource springJpaDataSource()
{
JndiDataSourceLookup lookup = new JndiDataSourceLookup();
return lookup.getDataSource("java:app/pos");
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean()
{
Map<String, Object> properties = new Hashtable<>();
properties.put("javax.persistence.schema-generation.database.action",
"none");
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabasePlatform("org.hibernate.dialect.MySQL5InnoDBDialect");
LocalContainerEntityManagerFactoryBean factory =
new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(adapter);
factory.setDataSource(this.springJpaDataSource());
factory.setPackagesToScan("my.package.entity");
factory.setSharedCacheMode(SharedCacheMode.ENABLE_SELECTIVE);
factory.setValidationMode(ValidationMode.NONE);
factory.setJpaPropertyMap(properties);
return factory;
}
The error is as below:
"Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactoryBean' defined in class sunwell.pos.service.config.RootContextConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean sunwell.pos.service.config.RootContextConfiguration.entityManagerFactoryBean()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springJpaDataSource' defined in class sunwell.pos.service.config.RootContextConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource sunwell.pos.service.config.RootContextConfiguration.springJpaDataSource()] threw exception; nested exception is org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'java:app/pos'; nested exception is org.springframework.jndi.TypeMismatchNamingException: Object of type [class com.sun.enterprise.naming.impl.SerialContext] available at JNDI location [java:app/pos] is not assignable to [javax.sql.DataSource]. Please see server.log for more details."
EDIT:
I have found that during deployment, the jndi name must be prefixed with jdbc
or something after the namespace, example: java:app/jdbc/pos
and not just java:app/pos
, however I'm not sure why I can run it with eclipse even without the jdbc
prefix and in JEE application I also can reference the jndi name in persistence like this <jta-data-source>java:app/pos</jta-data-source>
.
So bottom line is this problem only occurs during Spring war deployment. And the solution is you need to prefix the jndi name after the namespace with jdbc
or something.