Cannot get JDBC Connection in Elastic Beanstalk - java

i want to deploy my application in AWS.
i have setup my env.
and in root-context.xml of my application i have setup data source.
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#mydbinstance.cnn31xputfwg.ap-northeast-2.rds.amazonaws.com:1521:orcl" />
<property name="username" value="myid" />
<property name="password" value="mypassword" />
</bean>
this setting works fine locally in tomcat server.
but in AWS, it throws this error message
nested exception is org.apache.ibatis.exceptions.PersistenceException:
Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is
java.sql.SQLException: Cannot load JDBC driver class 'oracle.jdbc.driver.OracleDriver'
The error may exist in file [/var/lib/tomcat8/webapps/ROOT/WEB-INF/classes/mappers/emp/emp-mapper.xml]
The error may involve EmpDAO.List ### The error occurred while executing a query
Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is
java.sql.SQLException: Cannot load JDBC driver class 'oracle.jdbc.driver.OracleDriver'
Now my question is: what should i do to use get jdbc connection in AWS?

Looks like when you run the app on AWS it lacks Oracle JDBC driver in its CLASSPATH. So the answer is to add the driver to the classpath.
And I foresee the next question: "how to do this?", and simplest way is just to copy the driver to your classpath manually.
If you don't like manual work, you can generate Spring application template using Spring Initializr. Its build script has org.springframework.boot plugin, which does two important things:
builds single jar file which includes Tomcat, so the application can be started just by java -jar my-app.jar (avoiding deployment to tomcat)
includes all dependencies from build-script dependencies section to this jar file (including Oracle JDBC driver)

Related

How to configure HikariCP with PostgreSQL

I am trying to integrate HikariCP with PostgreSQL in my web application,I am using the postgresql driver and this DataSource class: org.postgresql.ds.PGSimpleDataSource and my hibernate configuration file is this:
<property name ="hibernate.connection.provider_class"> com.zaxxer.hikari.hibernate.HikariConnectionProvider</property>
 <property name ="hibernate.hikari.dataSourceClassName"> org.postgresql.ds.PGSimpleDataSource</property>
 <property name ="hibernate.hikari.dataSource.url">jdbc:postgresql:// localhost:5432/database</property>
 <property name ="hibernate.hikari.dataSource.user">user</property>
 <property name ="hibernate.hikari.dataSource.password">passwd</property>
 <property name ="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
 <property name="hibernate.current_session_context_class">thread</property>
When I run my application i have this exception:
org.hibernate.HibernateException: java.lang.RuntimeException: Property url does not exist on target class org.postgresql.ds.PGSimpleDataSource`
I read related information about this and what I did was replace the versions using the postgresql-9.4.1208 driver and hikaricp 2.4.6.

java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.PoolableConnection cannot be cast to oracle.jdbc.OracleConnection

In my application, I am continously getting this execption.
org.springframework.dao.InvalidDataAccessApiUsageException: OracleLobCreator needs to work on [oracle.jdbc.OracleConnection], not on
[org.apache.tomcat.dbcp.dbcp.PoolableConnection]: specify a corresponding NativeJdbcExtractor; nested exception is java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.PoolableConnection cannot be cast to oracle.jdbc.OracleConnection
at org.springframework.jdbc.support.lob.OracleLobHandler$OracleLobCreator.createLob(OracleLobHandler.java:547) ~[spring-jdbc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.jdbc.support.lob.OracleLobHandler$OracleLobCreator.setClobAsString(OracleLobHandler.java:447)
Here is my spring bean definition for lobHandler.
<bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler"
p:nativeJdbcExtractor-ref="nativeJdbcExtractor"/>
<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.C3P0NativeJdbcExtractor"/>
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"
p:lobHandler-ref="oracleLobHandler" p:dataSource-ref="dataSource"
p:databaseType="oracle" p:tablePrefix="BATCH_"
p:transactionManager-ref="transactionManager"
p:isolationLevelForCreate="ISOLATION_READ_COMMITTED"/>
I having, ojdbc6-12.1.0.1 jar in tomcat lib folder as well in my classpath. I tried few google solutions, but nothing seems working.
Any suggesstions
You're using c3po native JDBC extractor where you should probably be using DBCP native jdbc extractor.

Spring RMI error java.lang.ClassNotFoundException: org.springframework.remoting.rmi.RmiInvocationHandler

I am trying to create some beans using Spring and export them to RMI...
Here is my code:
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="OfferService" />
<property name="service" ref="offerService" />
<property name="serviceInterface" value="ro.project.services.OfferService" />
<property name="registryPort" value="1199" />
</bean>
I created a file called "policy.all" in my root folder and I am running my VM with that arguments but I still have this error:
java.lang.ClassNotFoundException: org.springframework.remoting.rmi.RmiInvocationHandler (no security manager: RMI class loader disabled)
I don't know what to do... in linux, the exact same project is running ok (with jdk 1.7.0.4) but in windows not... in java 1.5 (windows) it's working.. but in java 1.7.0.4 (windows) it's not working...
EDITED:
My error is:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.remoting.rmi.RmiServiceExporter#0' defined in class path resource [spring/services.xml]: Invocation of init method failed; nested exception is java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: org.springframework.remoting.rmi.RmiInvocationHandler (no security manager: RMI class loader disabled)
After adding this lines:
if (System.getSecurityManager() == null)
{
RMISecurityManager manager = new RMISecurityManager();
System.setSecurityManager(manager);
}
I have this error:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring/application-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring/application-context.xml] cannot be opened because it does not exist
thank you in advance
The point here is not to install a security manager but to find the class. You are missing some JAR file from your deployment.
I don't think it still important, but i personally face today the same issue, and cannot find answer. I post it here, maybe it helps someone.
Solution: stop rmiregistry program, or pass Spring jars to rmiregistry with -J option. Simplest way just stop rmiregistry and allow Spring to start another one with all required classes. And don't set up RMISecurityManager.
You need to have this in a part of your code:
System.setSecurityManager(new RMISecurityManager());
You can have another security manager as well, but there just has to be a security manager installed.
Actually the line I posted can be easily fired in the main class of a standalone desktop application, but I am not sure in your case. You should look for a configuration option doing this probably.
With Spring RMIServiceExporter, the Best Practice to manage correctly the RMI Registry is the use of RMIRegistryFactoryBean. To better permits you to understand, I paste an example of Spring configuration with a RemoteBean:
<bean id="RemoteServices" class="remote.RMIServicesImpl"/>
<bean id="RemoteRmiRegistry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
<property name="alwaysCreate" value="true" />
<property name="port" value="1093"></property>
</bean>
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="RemoteServices" />
<property name="serviceInterface" value="remote.RemoteInterface" />
<property name="serviceName" value="RemoteServices" />
<property name="replaceExistingBinding" value="true"></property>
<property name="registry" ref="RemoteRmiRegistry"></property>
</bean>
In this way the RmiRegistryFactoryBean will manage autonomously everything about the RMI Socket Layer, and you don't need to stop the JVM, but simply redeploy it.
Please rate me if it works.

Problems using Hibernate - JDBC Driver class not found: com.mysql.jdbc.Driver

I have a really strange issue when using hibernate to connect to a MySQLDB and add data.
This is the error I get:
JDBC Driver class not found:
com.mysql.jdbc.Driver
This is how my hibernate.cfg.xml looks like
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/fpa-webapp</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
</session-factory>
</hibernate-configuration>
I dont understand why I see a 500 Error when I navigate to the application; it says that the driver is not found.
HTTP ERROR 500
Problem accessing /fpa-webapp/.
Reason:
Exception constructing service 'ValueEncoderSource': Error invoking
service builder method
org.apache.tapestry5.services.TapestryModule.buildValueEncoderSource(Map,
InvalidationEventHub) (at
TapestryModule.java:2287) (for service
'ValueEncoderSource'): Error invoking
service contribution method
org.apache.tapestry5.hibernate.HibernateModule.contributeValueEncoderSource(MappedConfiguration,
boolean, HibernateSessionSource,
Session, TypeCoercer, PropertyAccess,
LoggerSource): Exception constructing
service 'HibernateSessionSource':
Error invoking service builder method
org.apache.tapestry5.hibernate.HibernateCoreModule.buildHibernateSessionSource(Logger,
List, RegistryShutdownHub) (at
HibernateCoreModule.java:123) (for
service 'HibernateSessionSource'):
JDBC Driver class not found:
com.mysql.jdbc.Driver
I'm sure the driver is in the class path.
What it could be?
Your driver is not on the classpath.
There are two ways to ensure it's on the classpath:
Add it to the global lib directory. For Tomcat this is TOMCAT_HOME/lib.
Include it in the war.
It depends on your requirements which you use.
If you're going to use Tomcat to manage the connection pool, you'll need to add it to the TOMCAT_HOME/lib and instead of defining your datasource directly in the hibernate configuration, you'll reference it via jndi.
The only plausible explanation is that the Driver class is not on the CLASSPATH.
Check to make sure that the mysql-connector-java (or other relevant) jar is indeed in a place where it will get loaded. If you're 100% positive that it is, it might help to provide more information about how you know the class is being loaded, so that we can identify other possible causes.

"No suitable driver" problem with Hibernate3, PostgreSQL 8.3 and Java 5

Does anybody know what's going on here:
I run hibernate 3.2.6 against a PostgreSQL 8.3 (installed via fink) database on my Mac OS X. The setup works fine when I use Java 6 and the JDBC 4 driver (postgresql-8.3-603.jdbc4). However, I need this stuff to work with Java 5 and (hence) JDBC 3 (postgresql-8.3-603.jdbc3). When I change the jar in the classpath and switch to Java 5 (I do this in eclipse), I get the following error:
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
<Rows clipped for readability>
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:545)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:423)
What's the problem here? I cannot see it. Here is my hibernate configuration:
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:postgresql:test</property>
<property name="connection.username">postgres</property>
<property name="connection.password">p</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<mapping resource="com/mydomain/MyClass.hbm.xml"/>
</session-factory>
</hibernate-configuration>
EDIT: The longer, more usual form of the connection URL: jdbc:postgresql://localhost/test has the exact same behaviour.
The driver jar is definitely in the classpath, and I also do not manage to get any errors with this direct JDBC test code:
public static void main(String[] args) throws Exception {
Class.forName("org.postgresql.Driver");
Connection con=DriverManager.getConnection("jdbc:postgresql://localhost/test","postgres", "p");
}
I don't see you specifying the driver class in your Hibernate configuration. Try adding the following:
<hibernate-configuration>
<session-factory>
.
.
<property name="connection.driver_class">org.postgresql.Driver</property>
.
</session-factory>
</hibernate-configuration>
did you notice that the connection url is incomplete
<property name="connection.url">jdbc:postgresql:test</property>
as opposed to
<property name="connection.url">jdbc:postgresql://localhost/test</property>
You say "work with Java 5 and (hence) JDBC 3 (postgresql-8.3-603.jdbc3)." Maybe this is a faulty assumption.
The download page is confusing to me. It seems to imply that for Java 1.5 you need JDBC3 but it's not 100% clear. I'm not sure why the JDBC4 driver won't work with Java 1.5 (we use a DB2 JDBC4 driver with Java 1.5).
Have you tried the JDBC4 driver with Java 1.5?
One of the new JDBC4 features is automatic loading via the Service Provider mechanism. By including a META-INF/services/java.sql.Driver file in the jar file, there is no longer the need to do Class.forName(""). This only works with the 1.6 JVM.
I had the same problem "no suiteble driver found" using a servlet the solution to register a driver is:
Class driverClass = Class.forName("org.postgresql.Driver");
DriverManager.registerDriver((Driver) driverClass.newInstance());
Found the solution here:
http://www.java2s.com/Tutorial/Java/0340__Database/DriverManagergetDriversenumeratealltheloadedJDBCdrivers.htm
http://codingexplorer.wordpress.com/2009/09/06/%E2%80%9Cno-suitable-driver%E2%80%9D-for-postgresql/

Categories