This question already has an answer here:
DriverManager unable to return connection after Java 8 upgrade
(1 answer)
Closed 3 years ago.
I am trying to get the impala jdbc connection in an application deployed on Jboss 6;
below is the spring bean (ID1) datasource definition for same. I am getting the exception while connecting; please refer to the exception below.
It occurs with Java 1.8 version; if I change it to Java 1.7, it works fine.
The below exception occurs only when the application also tries to connect to Oracle DB using the spring bean (ID2). If Oracle connection ID2 is not used, impala connects successfully.
Can someone please help?
Exception :
==java.lang.NullPointerException
===Stack trace...
java.util.Hashtable.put(Unknown Source)
com.cloudera.jdbc.common.AbstractDriver.copyProperties(Unknown Source)
com.cloudera.jdbc.common.AbstractDriver.connect(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source) org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFrom
DriverManager(DriverManagerDataSource.java:173) org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFrom
Driver(DriverManagerDataSource.java:164) org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnect onFromDriver(AbstractDriverBasedDataSource.java:149) org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:119) org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
I tried to debug into AbstractDriver.copyProperties(), the values in properties are as follows -
{user=myuser, password=pwd}
beans defined in Spring-config.xml
<bean id="ID1"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.cloudera.impala.jdbc41.Driver" />
<property name="url" value="jdbc:impala://impalahost:21050/;AuthMech=1;KrbRealm=myrealm;KrbServiceName=impala;KrbHostFQDN=xxx" />
</bean>
<bean id="ID2"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"
/>
<property name="url"
value="jdbc:oracle:thin:#//oraclehost:1523/DB" />
<property name="username" value="myuser" />
<property name="password" value="pwd" />
<property name="connectionProperties">
<props>
<prop key="defaultRowPrefetch">5000</prop>
</props>
</property>
</bean>
your url property should be like <property name="url" value="jdbc:oracle:thin:#localhost:1521:ORCL"></property> not with the double slash
Related
I'm stumbled upon a problem while developing a Web Application based on Struts2 with Spring and Hibernate.
When I refresh the site a few times, for example 2-4 times, Hibernate is showing up an exception about Too many connections. I've tried to implement C3P0 Connection pool and have some problems with it
The hibernate.cfg.xml configuration:
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/axelle</property>
<property name="hibernate.connection.username">axelle</property>
<property name="hibernate.connection.password">dbpassword</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
applicationContext.xml
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:jdbc.properties"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"
p:connectionProperties="${jdbc.connectionProperties}"/>
<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
The log output is:
org.hibernate.exception.JDBCConnectionException: Cannot open connection
and:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
And this is how PROCESSLIST MySQL window looks: http://img844.imageshack.us/img844/3959/be69273cc2.png
I've set max_size of connections to 20, but it seems like it doesn't read the C3P0 configuration from file, cause from the screen we can see that number of connections is higher than 20, or maybe I'm doing something wrong, but where? I really need some help guys, I'll appreciate this, and thanks in advance.
Mention these property in your hibernate.cfg.xml file
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.max_statements">10</property>
<property name="hibernate.c3p0.min_size">10</property>
<property name="hibernate.c3p0.timeout">100</property>
Refer this link for better understanding: Configuration Properties
In your applicationContext, instead of using <bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource" ... try using <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" ...
The C3P0PooledDataSource will create a wrapped database connection using the specified DriverClassName, url, username and password
I am trying to create a spring-managed standalone pool for tomcat-dbcp using the version 7.0.30 of tomcat-dbcp.jar
However it seems the org.apache.tomcat.jdbc.DataSource class that is referred to in the Tomcat's documentation (http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html#Standalone)
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
destroy-method="close">
<property name="factory"
value="org.apache.tomcat.jdbc.pool.DataSourceFactory" />
<property name="type" value="javax.sql.DataSource" />
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/ym" />
<property name="username" value="admin" />
<property name="password" value="admin" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
</bean>
So this spring bean definition is correct according to Tomcat doc, however when I start the app up, I get CNF exception:
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.tomcat.jdbc.pool.DataSource] for bean with name 'dataSource' defined in class path resource [application-context.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.tomcat.jdbc.pool.DataSource
Am I being super stupid and missing the obvious here???
Class org.apache.tomcat.jdbc.pool.DataSource is included in tomcat-jdbc.jar, not tomcat-dbcp.jar. The Tomcat JDBC Connection Pool is a replacement for commons-dbcp, of which tomcat-dbcp is just a renamed version.
The jar file tomcat-jdbc.jar is not included in all Tomcat installations (e.g. it is not included in the standard Debian/Ubuntu package). You can download it from Maven Central or you can find it in the Tomcat binary distribution archive.
I tried to setup infinispan as 2nd level cache for hibernate in spring+tomcat based app.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${hibernate.connection.driver_class}"/>
<property name="url" value="${hibernate.connection.url}"/>
<property name="username" value="${hibernate.connection.username}"/>
<property name="password" value="${hibernate.connection.password}"/>
<property name="maxActive" value="${hibernate.connection.maxActive}"/>
<property name="maxIdle" value="${hibernate.connection.maxIdle}"/>
<property name="minIdle" value="${hibernate.connection.minIdle}"/>
<property name="maxWait" value="${hibernate.connection.maxWait}"/>
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager">
<bean class="com.arjuna.ats.jta.TransactionManager" factory-method="transactionManager"/>
</property>
<property name="userTransaction">
<bean class="com.arjuna.ats.jta.UserTransaction" factory-method="userTransaction"/>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.example"/>
<!---->
<property name="hibernateProperties" ref="db-properties"/>
</bean>
And the properties are:
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
hibernate.cache.region.factory_class=org.hibernate.cache.infinispan.InfinispanRegionFactory
I get an exception:
Caused by: org.infinispan.CacheException: This is transactional cache but no transaction manager could be found. Configure the transaction manager lookup properly.
How do I configure a transaction manager lookup?
The easy answer for spring is to add (inside id="sessionFactory" spring XML configuration) :
<property name="jtaTransactionManager" ref="transactionManager"/>
This is available in spring 3.2.2.RELEASE (it maybe available in older spring versions too, I would guess since spring 3.1.x)
This should achieve what Steve Ebersole points at. This causes Spring to provide a JtaPlatform via the class https://github.com/SpringSource/spring-framework/blob/master/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/ConfigurableJtaPlatform.java
See JavaDoc for class org.springframework.orm.hibernate4.LocalSessionFactoryBean (that you are using) concerning JTA usage.
Both Hibernate and Infinispan need to know about the TransactionManager. If you tell Hibernate about the TransactionManager, it will in turn tell Infinispan for you. I am guessing Spring has a way to tell Hibernate about the JTA setup, but I could not find it. Hibernate for its part (I see you are at least trying to use Hibernate 4) needs to be told about which org.hibernate.service.jta.platform.spi.JtaPlatform to use. org.hibernate.service.jta.platform.spi.JtaPlatform is the contract for Hibernate to know how to interact with the JTA environment.
I'm trying to pass the tests automatically generated by roo for the entity classes. When I run the tests against hsql, they all pass. But when i run the tests against Oracle, I get the following:
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'oracle.jdbc.driver.OracleDriver' for connect URL 'jdbc:localhost:1521:xe'
I have an oracle db local, I know the connection works.
The ojdbc14.jar file is in the lib directory of JBoss 4.2.3/server/all directory (which is where we're deploying to)
This is the data source bean and entity manager factory definition
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="${database.driverClassName}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="1800000"/>
<property name="numTestsPerEvictionRun" value="3"/>
<property name="minEvictableIdleTimeMillis" value="1800000"/>
</bean><!-- Development specific configuration comes here. -->
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="persistenceUnitName" value="${hibernate.persistenceunit}"/>
<property name="dataSource" ref="dataSource"/>
</bean>
This is the pom dependency definition:
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.3.0</version>
<classifier/>
</dependency>
The persistenceunit variable resolves to persistenceUnitDev
this is the hibernate definition of that persistence unit
org.hibernate.ejb.HibernatePersistence
And finally my property file that defines the data connection:
database.password=password
database.url=jdbc\:localhost\:1521\:xe
database.username=username
database.driverClassName=oracle.jdbc.driver.OracleDriver
I can't figure out why it tells me it can't get a suitable driver.
thanks in advance
Your URL does not have the proper pattern. Try jdbc:oracle:thin:#localhost:1521:xe instead.
I'm trying to deploy an app using Eclipselink as my JPA Layer on IBM Websphere 7.0.0.9. While trying to do any CRUD operations, i get the following exception:
Caused by: java.lang.IllegalArgumentException: Object: Entity is not a known entity type.
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4199)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:380)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:600)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:358)
at $Proxy93.persist(Unknown Source)
... 89 more
Another stack trace i see is :
Caused by: java.lang.ClassCastException: Entity incompatible with Entity
at o.u.d.dao.jpa.converter.impl.EntityBeanConvertorImpl.convertToModel(EntityBeanConvertorImpl.java:143)
... 223 more
My persistence.xml looks like:
<persistence-unit name="ds" transaction-type="JTA"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <jta-data-source>dsjta</jta-data-source> <class>Entity</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="eclipselink.target-server" value="WebSphere_7" /> <property name="eclipselink.logging.level" value="OFF" /> <property name="eclipselink.ddl-generation" value="none" /> <property name="eclipselink.ddl-generation.output-mode" value="database" /> </properties> </persistence-unit>
My application context file looks like:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="ds"/> <property name="dataSource" ref="dataSource"/> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"> <property name="showSql" value="false"/> <property name="generateDdl" value="false"/> </bean> </property> <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/> </property> </bean>
Our VM is started with the -javaagent parameter specified and pointing to spring-agent.jar What could be the reason for these exceptions?
The issue here is there is no Spring LoadTimeWeaver for Websphere. When your application attempts to combine a Web-Tier application with the Spring managed EM while using the InstrumentationLoadTimeWeaver class cast exceptions result. EclipseLink supports weaving in all JPA 2 compliant containers but in this case Spring is acting as an intermediary and interfering with the weaving.
Until Spring has a LoadTimeWeaver for Websphere You will need to remove the InstrumentationLoadTimeWeaver and set the EclipseLink persistence.xml property "eclipselink.weaving" to false or use the Static weaver.