Getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver Exception - spring

I am getting Following Exception while configuring the Connection Pool in Tomcat
This is Context.xml
<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">
<!-- maxActive: Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to -1 for no limit.
-->
<!-- maxIdle: Maximum number of idle dB connections to retain in pool.
Set to -1 for no limit. See also the DBCP documentation on this
and the minEvictableIdleTimeMillis configuration parameter.
-->
<!-- maxWait: Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<!-- username and password: MySQL dB username and password for dB connections -->
<!-- driverClassName: Class name for the old mm.mysql JDBC driver is
org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
-->
<!-- url: The JDBC connection url for connecting to your MySQL dB.
-->
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="password" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql:///BUSINESS"/>
</Context>
This is Bean Entry
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/TestDB"></property>
<property name="resourceRef" value="true"></property>
</bean>
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Co
nnection; nested exception is org.apache.tomcat.dbcp.dbcp.SQLNestedException: Ca
nnot load JDBC driver class 'com.mysql.jdbc.Driver'
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(Dat
aSourceUtils.java:82)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:
382)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:45
8)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:46
6)
at com.businesscaliber.dao.Dao.getQueryForListMap(Dao.java:66)
at com.businesscaliber.dao.MiscellaneousDao.getDefaultSucessStory(Miscel
laneousDao.java:109)
at com.businesscaliber.listeners.BusinessContextLoader.contextInitialize
d(BusinessContextLoader.java:40)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContex
t.java:3795)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4
252)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase
.java:760)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:74
0)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:831)
at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:720
)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:490
)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1150)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java
:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(Lifecycl
eSupport.java:120)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443
)
at org.apache.catalina.core.StandardService.start(StandardService.java:4
48)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:700
)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
Caused by: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driv
er class 'com.mysql.jdbc.Driver'
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDat
aSource.java:1136)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSo
urce.java:880)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(D
ataSourceUtils.java:113)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(Dat
aSourceUtils.java:79)
... 30 more
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDat
aSource.java:1130)

Nothing complex here - you have mysql configured, but the jdbc driver for mysql either you never installed or never placed in the classpath.
Ignore/disable spring, hibernate, connection pools and just try this java code in a jsp or whatever.
Class.forName("com.mysql.jdbc.Driver");
Until that works (eg file is in class path), you will continue to have sadness

You must do the following steps:
1) Configuration of Spring beans
--------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="database" value="MYSQL" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
</bean>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"
scope="singleton">
<property name="jndiName" value="jdbc/jpaExamples" />
<property name="resourceRef" value="true" />
</bean>
<bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Definition of your beans -->
</beans>
2) Definition of DataSource resource in web-xml:
------------------------------------------------
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/jpaExamples</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3) Definition of Resource in Context of server.xml
--------------------------------------------------
<Context docBase="WebServiceRequestPersistence_Web3" path="/WebServiceRequestPersistence_Web3" debug="5" reloadable="true" crossContext="true" source="org.eclipse.jst.jee.server:WebServiceRequestPersistence_Web3">
<Resource
name="jdbc/jpaExamples"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="myUsername"
password="myPassword"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/jpaExamples"
/>
</Context>
4) Copy of JDBC driver jar into lib of Apache Tomcat 6
------------------------------------------------------
Copy your MySQL Java connector (for example: mysql-connector-java-5.1.6.jar) in the lib sub-directory of Apache Tomcat 6 installation.

If you are using MyEclipse and you are trying to run it from MyEclipse Tomcat;
Go to preferences (Window > Preferences), then Servers > Integrated Sandbox > MyEclipse Tomcat > Paths
Under prepend to calsspath, add the jar.

Related

spring db2 jndi datasource - ClassCastException

I've been spending some time today trying to specify a websphere 6.1 db2 jndi data source in my spring mvc 3.0 application config. I've followed a guide a got at http://www.ibm.com/developerworks/websphere/techjournal/0609_alcott/0609_alcott.html.
When I do this....
web.xml
<resource-ref>
<res-ref-name>jdbc/reportingManagerDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<resource-ref>
<res-ref-name>jdbc/reportingManagerCustDbDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
spring files
<bean id="ePosDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/reportingManagerDataSource"/>
<property name="lookupOnStartup" value="false"/>
<property name="cache" value="true" />
<property name="proxyInterface" value="javax.sql.DataSource" />
</bean>
<jee:jndi-lookup id="ePosDataSource"
jndi-name="jdbc/reportingManagerDataSource"
cache="true"
resource-ref="true"
lookup-on-startup="false"
proxy-interface="javax.sql.DataSource"/>
<bean id="custDbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/reportingManagerCustDbDataSource"/>
<property name="lookupOnStartup" value="false"/>
<property name="cache" value="true" />
<property name="proxyInterface" value="javax.sql.DataSource" />
</bean>
<jee:jndi-lookup id="custDbDataSource"
jndi-name="jdbc/reportingManagerCustDbDataSource"
cache="true"
resource-ref="true"
lookup-on-startup="false"
proxy-interface="javax.sql.DataSource"/>
I get the error ...
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: DSRA8101E: DataSource class cannot be used as one-phase: ClassCastException: com.ibm.db2.jcc.DB2XADataSource incompatible with javax.sql.ConnectionPoolDataSource
I have been frantically googling away to try and find a solution but have been unsuccessful. Can someone show me what is required to successfully set up my datasource please?
thanks

Issue with JMS + AOP + JNDI Resources

Hi and happy new year
I'm working on a project which need to implement jms, aop and some jndi resources.
So far, the project works fine when there are only jms and jndi but when i activated aop, i had some troubles.. here is the configuration :
<!-- JMS implementation -->
<bean id="jmsRefConnectionFactory.activemq" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jms/activeMQConnectionFactory" />
<property name="lookupOnStartup" value="false" />
<property name="proxyInterface" value="javax.jms.QueueConnectionFactory" />
</bean>
<bean id="jmsRefQueue.activemq" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jms/activeMQQueue" />
<property name="lookupOnStartup" value="false" />
<property name="proxyInterface" value="javax.jms.Queue" />
</bean>
<bean id="jmsConnectionFactory.activemq" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg ref="jmsRefConnectionFactory.activemq" />
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsConnectionFactory.activemq" />
<property name="defaultDestination" ref="jmsRefQueue.activemq" />
<property name="destinationResolver" ref="jmsDestinationResolver.amq" />
<property name="sessionTransacted" value="true" />
<property name="sessionAcknowledgeMode" value="#{T(javax.jms.Session).CLIENT_ACKNOWLEDGE}" />
</bean>
<bean id="transactionManager" class="org.springframework.jms.connection.JmsTransactionManager">
<property name="connectionFactory" ref="jmsRefConnectionFactory.activemq" />
</bean>
<bean id="jmsDestinationResolver.amq"
class="org.springframework.jms.support.destination.DynamicDestinationResolver" />
When I start the application, I get this error :
Caused by: org.springframework.aop.AopInvocationException: AOP configuration seems to be invalid: tried calling method [public abstract javax.jms.Connection javax.jms.ConnectionFactory.createConnection() throws javax.jms.JMSException] on target [org.apache.activemq.ActiveMQConnectionFactory#239f 6]; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class
Context.xml content :
<!-- APACHE MQ -->
<Resource name="jms/activeMQConnectionFactory" auth="Container"
type="org.apache.activemq.ActiveMQConnectionFactory" description="JMS Connection Factory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory" HOST="localhost"
PORT="61616" CHAN="" TRAN="1" QMGR="MyQCF" />
<Resource name="jms/activeMQQueue" auth="Container"
type="org.apache.activemq.command.ActiveMQQueue" description="my Queue"
factory="org.apache.activemq.jndi.JNDIReferenceFactory" physicalName="userQueue" />
Web.xml content :
<resource-ref>
<res-ref-name>jms/activeMQConnectionFactory</res-ref-name>
<res-type>javax.jms.QueueConnectionFactory</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<resource-ref>
<res-ref-name>jms/activeMQQueue</res-ref-name>
<res-type>javax.jms.Queue</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
This configuration is OK when aop is not activated but for some reasons, it doesn't work when aop is on..
Spring version : 3.1.2
I'm using activemq (5.7)
I found out what was the problem..
Turns out the aop was not even involved (at least not the configuration that i put). I had some jar in my tomcat server that was not taken for some reasons (i think it's about lib priority/ parent first/last)...
I changed the location and directly put into my project and it works now ! :)

Persistance in JBoss wildfly (in a spring application)

I have a spring 3.2 application which has defined persisence with the following configuration:
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL"/>
<property name="showSql" value="false"/>
<property name="generateDdl" value="false"/>
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/>
</bean>
<util:map id="jpaPropertyMap">
<entry key="hibernate.hbm2ddl.auto" value="update"/>
<entry key="hibernate.cache.use_query_cache" value="true" />
<entry key="hibernate.max_fetch_depth" value="4" />
<entry key="hibernate.show_sql" value="false" />
<entry key="hibernate.format_sql" value="false" />
<entry key="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />
<entry key="hibernate.transaction.factory_class" value="org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory" />
<entry key="hibernate.connection.autocommit" value="false" />
</util:map>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="jtaDataSource" ref="dataSource" />
<property name="jpaPropertyMap" ref="jpaPropertyMap" />
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
</bean>
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager" >
<property name="packagesToScan" value="gr.hcg.nautologhsh.domain" />
<property name="defaultJtaDataSource" ref="dataSource" />
</bean>
My entity classes exist under the gr.hcg.nautologhsh.domain package. I am not using any persistance.xml files.
Now, this configuration is working fine in JBoss 8 wildfly. However when I see the Administration console of JBoss, in the tag JPA, I don't see any persistance units.
From some googling I think that the persistence units have to be configured through JBoss and not through Spring in order to be visible there - is that true ? I can't really understand why there could be different PUs in JBoss - how can they be used ? The documentation of JBoss is very spartian in that subject and I couldn't understand many things :(
Can somebody explain to me how can I define JBoss persistence units and show these in the JPA tag of wildfly's administration console ?
Update
I created a persistence.xml with the following contents:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="nautologhshPU" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/nautologhsh</jta-data-source>
</persistence-unit>
</persistence>
changed my datasource-config.xml beans like ths:
<jee:jndi-lookup id="dataSource" jndi-name="java:jboss/datasources/nautologhsh" resource-ref="false" />
<tx:jta-transaction-manager />
<jee:jndi-lookup jndi-name="java:comp/env/jpa/EntityManager" id="entityManager" expected-type="javax.persistence.EntityManager" />
and added the following to web.xml
<persistence-context-ref>
<persistence-context-ref-name>jpa/EntityManager</persistence-context-ref-name>
<persistence-unit-name>nautologhshPU</persistence-unit-name>
</persistence-context-ref>
And now get the following error:
20:24:41,839 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 52) MSC000001: Failed to start service jboss.persistenceunit."Nautologhsh.war#nautologhshPU": org.jboss.msc.service.StartException in service jboss.persistenceunit."Nautologhsh.war#nautologhshPU": org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.service.classloading.spi.ClassLoaderService]
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:169) [wildfly-jpa-8.0.0.Beta1.jar:8.0.0.Beta1]
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:117) [wildfly-jpa-8.0.0.Beta1.jar:8.0.0.Beta1]
at java.security.AccessController.doPrivileged(Native Method) [rt.jar:1.7.0_45]
at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:463) [wildfly-security-manager-1.0.0.Beta3.jar:1.0.0.Beta3]
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:178) [wildfly-jpa-8.0.0.Beta1.jar:8.0.0.Beta1]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_45]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_45]
at java.lang.Thread.run(Thread.java:744) [rt.jar:1.7.0_45]
at org.jboss.threads.JBossThread.run(JBossThread.java:122) [jboss-threads-2.1.1.Final.jar:2.1.1.Final]
Caused by: org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.service.classloading.spi.ClassLoaderService]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:127)
at org.hibernate.envers.event.EnversIntegrator.integrate(EnversIntegrator.java:65)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:310)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1837)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:854)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:847)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:396)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:846)
at org.jboss.as.jpa.hibernate4.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:151) [wildfly-jpa-8.0.0.Beta1.jar:8.0.0.Beta1]
... 8 more
20:24:41,861 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "Nautologhsh.war")]) - failure description: {"JBAS014671: Failed services" => {"jboss.persistenceunit.\"Nautologhsh.war#nautologhshPU\"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"Nautologhsh.war#nautologhshPU\": org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.service.classloading.spi.ClassLoaderService]
Caused by: org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.service.classloading.spi.ClassLoaderService]"}}
Does any body know what is this org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.service.classloading.spi.ClassLoaderService] ??
You can take a look at this quickstart example: https://github.com/sgilda/wildfly-quickstart/blob/master/greeter-spring which uses a META-INF/persistence.xml file an in which Spring configuration uses lookups to retrieve the EntityManager and the Datasource from Wildfly:
<!-- Look up the database in JNDI -->
<jee:jndi-lookup jndi-name="java:jboss/datasources/ExampleDS" id="dataSource"
expected-type="javax.sql.DataSource" />
<!-- Look up the container deployed EntityManager -->
<jee:jndi-lookup jndi-name="java:comp/env/hello/EntityManager"
id="entityManager" expected-type="javax.persistence.EntityManager" />
(https://github.com/sgilda/wildfly-quickstart/blob/master/greeter-spring/src/main/webapp/WEB-INF/spring-business-context.xml)

Unable to use JNDI DataSource provided by Tomcat in Spring?

I want to use JNDI DataSource provided by Tomcat in Spring based application. I use Tomcat 7 pool. Tried to configure as described here.
Configured server.xml of Tomcat:
<GlobalNamingResources>
<Resource name="jdbc/ApsuserAtAzistst"
auth="Container"
type="org.apache.tomcat.jdbc.pool.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:#10.0.153.10:1525:AZISTST"
username="APSUSER"
password="PASSWORDOFAPSUSER"
initialSize="1"
minIdle="1"
maxIdle="1"
maxActive="3"
maxWait="1000"
validationQuery="select 1 from dual"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport(threshold=1500)"
/>
</GlobalNamingResources>
created META-INF\context.xml with content:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<ResourceLink name="jdbc/ApsuserAtAzistst"
global="jdbc/ApsuserAtAzistst"
type="org.apache.tomcat.jdbc.pool.DataSource"/>
</Context>
and configured applicationContext.xml
<beans profile="dev,test,default">
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/ApsuserAtAzistst"/>
</bean>
</beans>
When I run applcation I receive an error:
SEVERE: Exception processing Global JNDI Resources
javax.naming.NamingException: Cannot create resource instance
at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:146)
at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source)
at org.apache.naming.NamingContext.lookup(NamingContext.java:843)
at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
at org.apache.naming.NamingContextBindingsEnumeration.nextElementInternal(NamingContextBindingsEnumeration.java:119)
at org.apache.naming.NamingContextBindingsEnumeration.next(NamingContextBindingsEnumeration.java:73)
at org.apache.naming.NamingContextBindingsEnumeration.next(NamingContextBindingsEnumeration.java:36)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:140)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:147)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:112)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.lifecycleEvent(GlobalResourcesLifecycleListener.java:84)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:402)
at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:347)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:725)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.startup.Catalina.start(Catalina.java:684)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:456)
Everything works when I change applicationContext.xml to:
<beans profile="dev,test,default">
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:#10.0.153.10:1525:AZISTST"/>
<property name="username" value="APSUSER"/>
<property name="password" value="PASSWORDOFAPSUSER"/>
<property name="initialSize" value="1"/>
<property name="minIdle" value="1"/>
<property name="maxIdle" value="1"/>
<property name="maxActive" value="3"/>
<property name="maxWait" value="1000"/>
<property name="validationQuery" value="select 1 from dual"/>
<property name="jdbcInterceptors"
value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport(threshold=1500)"/>
</bean>
</beans>
But I want to configure JNDI DataSource in Tomcat and use it.
The reason was that, I missed factory="org.apache.tomcat.jdbc.pool.DataSourceFactory". Right definition of resource must be
<GlobalNamingResources>
<Resource name="jdbc/ApsuserAtAzistst"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:#10.0.153.10:1525:AZISTST"
username="APSUSER"
password="PASSWORDOFAPSUSER"
initialSize="1"
minIdle="1"
maxIdle="1"
maxActive="3"
maxWait="1000"
validationQuery="select 1 from dual"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport(threshold=1500)"
/>
</GlobalNamingResources>

Jboss 7 - Spring - using Jboss TransactionManager

Using: Jboss 7.1.1, Hibernate 3.6.9, Spring 3.0.1.
I tried out to switch from Spring Transaction Manager:
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
to Jboss Transaction Manager:
<!-- search for transaction managers -->
<tx:jta-transaction-manager/>
Using org.springframework.orm.jpa.JpaTransactionManager all works fine but if I remove "transactionManager" bean and added "" I get following error:
javax.persistence.TransactionRequiredException: no transaction is in progress
I found following in the server log:
08:40:58,382 INFO [org.hibernate.transaction.TransactionFactoryFactory] (MSC service
thread 1-1) Using default transaction strategy (direct JDBC transactions)
08:40:58,383 INFO [org.hibernate.transaction.TransactionManagerLookupFactory] (MSC
service thread 1-1) No TransactionManagerLookup configured (in JTA
environment, use of read-write or transactional second-level cache is not
recommended)
What that means? Does my application using java:jboss/TransactionManager now or not? Do I have to configure something on Jboss?
applicationContext.xml snippet:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="xxx" />
<property name="dataSource" ref="dataSource" />
<property name="persistenceXmlLocation" value="classpath*:META-
INF/persistence.xml" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform"
value="org.hibernate.dialect.SQLServerDialect" />
<property name="showSql" value="false" />
<property name="generateDdl" value="false" />
</bean>
</property>
</bean>
<tx:jta-transaction-manager/>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${database.jndi.path}" />
persistence.xml snippet:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="xxx" transaction-type="RESOURCE_LOCAL">
<!-- a lot of <class>com.MyClass</class> -->
<properties>
<property name="jboss.as.jpa.managed" value="false"/>
<property name="hibernate.dialect"
value="org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect"/>
</properties>
</persistence-unit>
Maybe "RESOURCE_LOCAL" is wrong in persitence.xml?
domain.xml:
<datasource jta="true" jndi-name="java:jboss/datasources/myName" pool-name="myName" use-ccm="true">
<connection-url>jdbc:sqlserver://10.100.0.91:1433;databaseName=db;user=myUser;password=myPassword</connection-url>
<driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
<driver>mssql</driver>
<transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
<pool>
<min-pool-size>10</min-pool-size>
<max-pool-size>20</max-pool-size>
<prefill>true</prefill>
</pool>
<security>
<user-name>myName</user-name>
<password>myPassword</password>
</security>
<validation>
<validate-on-match>false</validate-on-match>
<background-validation>false</background-validation>
</validation>
<statement>
<share-prepared-statements>false</share-prepared-statements>
</statement>
</datasource>
--EDIT--
Now I changed my applicationContext.xml and persistence.xml!
applicationContext.xml snippet:
<jee:jndi-lookup id="my_unit" jndi-name="java:jboss/my_unit" />
<tx:jta-transaction-manager/>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${database.jndi.path}" />
</bean>
persistence.xml snippet:
?xml version="1.0" encoding="UTF-8" standalone="no"?>
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<jta-data-source>java:jboss/datasources/wtb</jta-data-source>
<properties>
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.JBossTransactionManagerLookup" />
<property name="hibernate.dialect"
value="org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect" />
</properties>
</persistence-unit>
Now I get following exception while deploying:
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: my_unit] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:162)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.start(PersistenceUnitServiceImpl.java:85)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
... 3 more
Caused by: org.hibernate.HibernateException: Dialect class not found: org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect
at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:76)
at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:64)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:176)
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2270)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2266)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1735)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:84)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
... 9 more
Caused by: org.hibernate.service.classloading.spi.ClassLoadingException: Unable to load class [org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect]
at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:141)
at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:73)
... 20 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect
at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl$1.findClass(ClassLoaderServiceImpl.java:99)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306) [rt.jar:1.6.0_32]
at java.lang.ClassLoader.loadClass(ClassLoader.java:247) [rt.jar:1.6.0_32]
at
org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:138)
... 21 more
Are you using this in your applicationContext:
<tx:annotation-driven />
And in your Method:
#Transaction
public void store(Message message){...}

Resources