mongodb on tomcat with spring - spring

I have written an RestAPI server that uses mongodb with spring, and successfully tested it locally. But, when I am deploying the war file on my server (tomcat) it causes an error.
After searching, I added "compile 'org.mongodb:mongo-java-driver:2.13.3'" to my build.gradle/pom.xml file based on a tutorial. But, I don't know what to do next to solve the error. I assume it has something to do with initializing a mongodb. Here is my error on Jenkins when it tries do deploys it:
BUILD SUCCESSFUL in 6s
4 actionable tasks: 4 executed
Build step 'Invoke Gradle script' changed build result to SUCCESS
Deploying /var/jenkins_home/workspace/AddressBook/build/libs/AddressBook.war to container Tomcat 8.x Remote with context
Redeploying [/var/jenkins_home/workspace/AddressBook/build/libs/AddressBook.war]
Undeploying [/var/jenkins_home/workspace/AddressBook/build/libs/AddressBook.war]
Deploying [/var/jenkins_home/workspace/AddressBook/build/libs/AddressBook.war]
ERROR: Build step failed with exception
org.codehaus.cargo.container.ContainerException: Failed to deploy [/var/jenkins_home/workspace//AddressBook/build/libs/AddressBook.war]
at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.deploy(AbstractTomcatManagerDeployer.java:106)
at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.redeploy(AbstractTomcatManagerDeployer.java:184)
at hudson.plugins.deploy.CargoContainerAdapter.deploy(CargoContainerAdapter.java:77)
at hudson.plugins.deploy.CargoContainerAdapter$DeployCallable.invoke(CargoContainerAdapter.java:147)
at hudson.plugins.deploy.CargoContainerAdapter$DeployCallable.invoke(CargoContainerAdapter.java:117)
at hudson.FilePath.act(FilePath.java:997)
at hudson.FilePath.act(FilePath.java:975)
at hudson.plugins.deploy.CargoContainerAdapter.redeploy(CargoContainerAdapter.java:114)
at hudson.plugins.deploy.PasswordProtectedAdapterCargo.redeploy(PasswordProtectedAdapterCargo.java:93)
at hudson.plugins.deploy.DeployPublisher.perform(DeployPublisher.java:64)
at hudson.tasks.BuildStepMonitor$3.perform(BuildStepMonitor.java:45)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
at hudson.model.Build$BuildExecution.post2(Build.java:186)
at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
at hudson.model.Run.execute(Run.java:1749)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Caused by: org.codehaus.cargo.container.tomcat.internal.TomcatManagerException: The Tomcat Manager responded "FAIL - Deployed application at context path [/AddressBook] but context failed to start
" instead of the expected "OK" message
at org.codehaus.cargo.container.tomcat.internal.TomcatManager.invoke(TomcatManager.java:715)
at org.codehaus.cargo.container.tomcat.internal.TomcatManager.deployImpl(TomcatManager.java:761)
at org.codehaus.cargo.container.tomcat.internal.TomcatManager.deploy(TomcatManager.java:312)
at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.deploy(AbstractTomcatManagerDeployer.java:101)
... 18 more
org.codehaus.cargo.container.tomcat.internal.TomcatManagerException: The Tomcat Manager responded "FAIL - Deployed application at context path [/AddressBook] but context failed to start
" instead of the expected "OK" message
at org.codehaus.cargo.container.tomcat.internal.TomcatManager.invoke(TomcatManager.java:715)
at org.codehaus.cargo.container.tomcat.internal.TomcatManager.deployImpl(TomcatManager.java:761)
at org.codehaus.cargo.container.tomcat.internal.TomcatManager.deploy(TomcatManager.java:312)
at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.deploy(AbstractTomcatManagerDeployer.java:101)
at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.redeploy(AbstractTomcatManagerDeployer.java:184)
at hudson.plugins.deploy.CargoContainerAdapter.deploy(CargoContainerAdapter.java:77)
at hudson.plugins.deploy.CargoContainerAdapter$DeployCallable.invoke(CargoContainerAdapter.java:147)
at hudson.plugins.deploy.CargoContainerAdapter$DeployCallable.invoke(CargoContainerAdapter.java:117)
at hudson.FilePath.act(FilePath.java:997)
at hudson.FilePath.act(FilePath.java:975)
at hudson.plugins.deploy.CargoContainerAdapter.redeploy(CargoContainerAdapter.java:114)
at hudson.plugins.deploy.PasswordProtectedAdapterCargo.redeploy(PasswordProtectedAdapterCargo.java:93)
at hudson.plugins.deploy.DeployPublisher.perform(DeployPublisher.java:64)
at hudson.tasks.BuildStepMonitor$3.perform(BuildStepMonitor.java:45)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
at hudson.model.Build$BuildExecution.post2(Build.java:186)
at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
at hudson.model.Run.execute(Run.java:1749)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Build step 'Deploy war/ear to a container' marked build as failure
Finished: FAILURE
My application is something like https://spring.io/guides/gs/accessing-data-mongodb/, with some RestAPI mappings.
And here is the tomcat log:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'per
sonController': Unsatisfied dependency expressed through field 'PersonRepo'; nested exception is org
.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepositor
y': Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating
bean with name 'mongoTemplate' defined in class path resource [org/springframework/boot/autoconfigu
re/data/mongo/MongoDataAutoConfiguration.class]: Unsatisfied dependency expressed through method 'mo
ngoTemplate' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependenc
yException: Error creating bean with name 'mongoDbFactory' defined in class path resource [org/sprin
gframework/boot/autoconfigure/data/mongo/MongoDataAutoConfiguration.class]: Unsatisfied dependency e
xpressed through method 'mongoDbFactory' parameter 0; nested exception is org.springframework.beans.
factory.BeanCreationException: Error creating bean with name 'mongo' defined in class path resource
[org/springframework/boot/autoconfigure/mongo/MongoAutoConfiguration.class]: Bean instantiation via
factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Fai
led to instantiate [com.mongodb.MongoClient]: Factory method 'mongo' threw exception; nested excepti
on is java.lang.IllegalAccessError: tried to access method org.bson.types.ObjectId.<init>(III)V from
class com.mongodb.Bytes
UPDATE (fix of above error):
So, I had this as dependency in my build.gradle:
compile 'org.mongodb:mongo-java-driver:2.13.3'
I changed it to (not enforcing version):
compile group: 'org.mongodb', name: 'mongo-java-driver'
But, now when I request a GET from my server, I see:
Sun Mar 04 21:05:23 UTC 2018
There was an unexpected error (type=Internal Server Error, status=500).
Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]
Here is my application.properties:
spring.data.mongodb.host=<server_address>
spring.data.mongodb.port=27017
spring.data.mongodb.user=<username>
MongoDB server on localhost:27017 (user , pwd: same as /var/jenkins_home).
Should I somehow tell my application about the path of mongoDB?
Or should I tell my application about the database name? (I think it will use some default one)

Related

Problems while restore nifi version 1.12.1 with old configs

In general, the update was successful. But after transferring the configs from the old version (authorizers.xml, nifi.properties, keystore.jks, flow.xml.gz) to the new version, it starts to swear that they are not valid, and the keystore password is incorrect, but open it with the old password successfully. Below presented errors in the log:
WARN [main] org.eclipse.jetty.webapp.WebAppContext Failed startup of context o.e.j.w.WebAppContext#6bb4dd34{nifi-api,/nifi-api,file:///opt/nginx/nifi-ift/nifi/work/jetty/nifi-web-api-1.12.1.war/webapp/,UNAVAILABLE}{./work/nar/framework/nifi-framework-nar-1.12.1.nar-unpacked/NAR-INF/bundled-dependencies/nifi-web-api-1.12.1.war}
org.apache.nifi.web.NiFiCoreException: Unable to start Flow Controller.
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'flowService': FactoryBean threw
exception on object creation; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'flowController': FactoryBean threw exception
on object creation; nested exception is
java.lang.IllegalStateException: Flow controller TLS configuration is
invalid
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'flowController': FactoryBean threw
exception on object creation; nested exception is
java.lang.IllegalStateException: Flow controller TLS configuration is
invalid
Caused by: java.lang.IllegalStateException: Flow controller TLS
configuration is invalid
Caused by: org.apache.nifi.security.util.TlsException: The truststore
properties are not valid
WARN [main] org.apache.nifi.web.server.JettyServer Failed to start web server... shutting down.
java.io.IOException: Keystore was tampered with, or password was incorrect
Caused by: java.security.UnrecoverableKeyException: Password verification failed

Spring can't connect to database when started by Monit

I have a Jetty 6 server running a grails 1.1 (which in turn uses hibernate) application. Since sometimes it goes down, I'd like it to automatically restart if something happens. So, I heard about Monit and decided to give it a try. I've created the following configuration:
check process jetty with pidfile /var/run/jetty.pid
group nfetron
start program = "/data/ebs/NFeTron/bin/jetty.sh start"
stop program = "/data/ebs/NFeTron/bin/jetty.sh stop"
if failed host "localhost" port 8090 protocol HTTP with timeout 10 seconds then restart
if mem usage > 95% for 11 cycles then restart
Well, the problem is that when the Monit starts the Jetty, it doesn't run normally. It fails to connect to the database, as seen in the following log:
2017-02-13 21:35:34.356::INFO: jetty-6.1.14
2017-02-13 21:35:34.404::INFO: Deploy /data/ebs/NFeTron/contexts/javadoc.xml -> org.mortbay.jetty.handler.ContextHandler#153f5a29{/javadoc,file:/data/ebs/NFeTron/java$
2017-02-13 21:35:34.536::INFO: No Transaction manager found - if your webapp requires one, please configure one.
2017-02-13 21:35:34.875::INFO: No Transaction manager found - if your webapp requires one, please configure one.
2017-02-13 21:35:35.404::INFO: No Transaction manager found - if your webapp requires one, please configure one.
2017-02-13 21:35:37.337:/NFeTron:INFO: Initializing Spring root WebApplicationContext
loading security config ...
2017-02-13 21:35:54.383::WARN: Failed startup of context org.mortbay.jetty.webapp.WebAppContext#770c2e6b{/NFeTron,file:/data/ebs/NFeTron/webapps/NFeTron/}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageSource': Initialization of bean failed; nested exception is org.springfr$
at java.security.AccessController.doPrivileged(Native Method)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactor$
at java.security.AccessController.doPrivileged(Native Method)
... 1 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception $
at java.security.AccessController.doPrivileged(Native Method)
... 2 more
Caused by: java.lang.NullPointerException
... 3 more
2017-02-13 21:35:54.384::WARN: Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageSource': Initialization of bea$
java.lang.NullPointerException
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessController.doPrivileged(Native Method)
2017-02-13 21:35:54.390::INFO: Opened /data/ebs/NFeTron/logs/2017_02_13.request.log
2017-02-13 21:35:54.456::INFO: Started SelectChannelConnector#0.0.0.0:8090
I searched a lot and couldn't find anyone facing the same problem. When I start the application using SSH (with root user), it works normally. But when Monit starts it (supposedly with root user), it doesn't. Does anyone have any clue about this problem?

Can't start application in IDE with <jar-find> error, but 'gradle bootRun' - works fine

So, i have a problem that can't solve.
I have a multi module gradle project with spring boot runners. With Jpa, Elastic, OrientDb and another configurations provided by spring-data and spring-integration. And i try to start my web application! Write web starter, make annotation scan, so when I use task 'gradle bootRun' - everything start perfectly - app works, all contexts up and life is good.BUT, when i try use my favorite IDE like Intellij idea, or colleague use Eclipse - get an error :
org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with
name 'org.springframework.integration.config.IdGenerato rConfigurer#0':
BeanPostProcessor before instantiation of bean failed; nested exception is
org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with
name org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration':
Initialization of bean failed; nested exception is java.lang.NoSuchMethodError:
org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration.setBeanFactory(Lo rg/springframework/beans/factory/BeanFactoryV
That cause by (projectName - is fake):
java.lang.RuntimeException: error trying to scan <jar-file>:
file:/home/<user>/project/<projectName>/classes/production/<projectName>-system-webapp/
But i have NO /projectName/classes/production/ folder! Anyway - with gradle it's work fine.
And to be honestly, before this another error catched:
Error creating bean with name 'entityManagerFactory' defined in class com.project.system.jpa.PepperJpaConfiguration: Invocation of init method failed
caused by:
error trying to scan <jar-file>: file:/home/<user>/project/<projectName>/classes/production/<projectName>-system-webapp/
Thank you for advice! Can give additional info.
P.S : Edited
So, the problem was solved:
I do not provide important error stacktrace like:
at org.hibernate.ejb.packaging.NativeScanner.getFilesInJar(NativeScanner.java:195)
at org.hibernate.ejb.Ejb3Configuration.addScannedEntries(Ejb3Configuration.java:506)
at org.hibernate.ejb.Ejb3Configuration.scanForClasses(Ejb3Configuration.java:861)
... 21 more
Caused by: java.io.IOException: invalid constant type: 18 at 50
And this was and JDK8-Hibernate-javassist problem https://hibernate.atlassian.net/browse/HHH-8286
When upgrade javassist version to 3.18.1-GA - all problems was solved(but why run through gradle worked perfectly...)

BundleContext is no longer valid

I am having a Java web application which runs in an OSGi container. I have deployed my application on a Tomcat server. When I restart my server sometimes I am randomly getting mutiple instaces of error "BundleContext is no longer valid" and hence I am unable to deploy the application. It doesn't happen all the time when we restart our application server but happens sometimes only. The only solution we found is just to restart the server. Following is the stack trace:
org.springframework.osgi.extender.internal.dependencies.startup.DependencyServiceManager] : Adding
OSGi service dependency for importer [&simplyticOsgiSaeAdapter] matching OSGi filter [(objectClass=c
om.moodysanalytics.sae.simplytic.adapter.SimplyticSaeAdapter)]
[org.springframework.osgi.service.importer.support.OsgiServiceProxyFactoryBean] : Creating a single
service proxy ...
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (
1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'dataSource
' threw exception; nested exception is java.lang.IllegalStateException: **BundleContext is no longer valid**
org.ops4j.pax.logging.pax-logging-api[org.springframework.beans.factory.xml.BeanDefinitionParserDele
gate] : No XML 'id' specified - using 'templateElement' as bean name and [] as aliases
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAcce
ssor.java:102)
[org.springframework.beans.factory.support.DefaultListableBeanFactory] : FactoryBean threw exception
from getObjectType, despite the contract saying that it should return null if the type of its objec

No suitable driver exception while grail tomcat deployment

I have deployed a grails application into tomcat container but am getting No suitable driver exception: can any one help in solving this.
SEVERE: Exception sending context initialized event to listener instance of class org.codehaus.groovy.grails.web.context.GrailsContextLoader Listener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve reference to bean 'h ibernateProperties' while setting bean property 'hibernateProperties'; nested exception is org.springframework.beans.factory.BeanCreationExc eption: Error creating bean with name 'hibernateProperties': Cannot resolve reference to bean 'dialectDetector' while setting bean property 'properties' with key [hibernate.dialect]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dialectDetector': Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'oracle.jdbc.OracleDriver' for connect URL 'jdbc:xxxx:oracle://xxxxx:1521;ServiceName=xxxx'>
at org.grails.datastore.gorm.plugin.support.PersistenceContextInterceptorAggregator.postProcessBeanFactory(PersistenceContextInterce ptorAggregator.groovy:104) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateProperties': Cannot resolve refe rence to bean 'dialectDetector' while setting bean property 'properties' with key [hibernate.dialect]; nested exception is org.springframewo rk.beans.factory.BeanCreationException: Error creating bean with name 'dialectDetector': Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is org.apache.commons .dbcp.SQLNestedException: Cannot create JDBC driver of class 'oracle.jdbc.OracleDriver' for connect URL 'jdbc:xxxx:oracle://xxxx :1521;xxxx=xxxx'
... 1 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dialectDetector': Invocation of init meth od failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exc eption is org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'oracle.jdbc.OracleDriver' for connect URL 'jdbc:in formatica:oracle://xxxx:1521;xxx=xxx'
... 1 more
Caused by: org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is org.apache .commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'oracle.jdbc.OracleDriver' for connect URL 'jdbc:xxx:oracle://xxxx:1521;xxxx=xxxx'
... 1 more
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'oracle.jdbc.OracleDriver' for connect URL 'jdbc: xxxx:oracle://xxxx:1521;xxxx=xxxx'
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
... 1 more Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:264)
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
... 3 more
May 30, 2013 7:31:25 PM org.apache.catalina.core.StandardContext start
SEVERE: Error listenerStart
May 30, 2013 7:31:25 PM org.apache.catalina.core.StandardContext start
SEVERE: Context [/Jarvis] startup failed due to previous errors
May 30, 2013 7:31:25 PM org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
UPDATED for a dependency definition example
Add the following repository definition to your BuildConfig.groovy
mavenRepo "https://code.lds.org/nexus/content/groups/main-repo"
In your dependencies section the following should appear:
runtime 'com.oracle:ojdbc6:11.2.0.3'
A better way is to install manually the Oracle driver to your corporate Repository(Artifactory, Nexus, Archiva, file server, etc.)

Resources