Spring can't connect to database when started by Monit - spring

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?

Related

How to ignore mongo when start application

A legacy Spring boot project, before it used mongo, but now it does not need. Now start this project it has this error
2022-10-27 14:10:13.119 [cluster-ClusterId{value='635a20bb714f8f71e2e5d041', description='null'}-10.48.51.21:40010] [INFO] [org.mongodb.driver.cluster] - Exception in monitor thread while connecting to server 10.48.51.21:40010
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70)
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127)
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
then I exclude related Auto Configuration class
#SpringBootApplication(exclude={MongoAutoConfiguration.class, MongoRepositoriesAutoConfiguration.class,MongoDataAutoConfiguration.class})
but this time project start failed
***************************
APPLICATION FAILED TO START
***************************
Description:
Field mongoTemplate in com.bxt.bmht_support.dao.BmhtMideTableDao required a bean of type 'org.springframework.data.mongodb.core.MongoTemplate' that could not be found.
How to disable mongo without update current code? just want ignore it.

Connection refused from neo4j docker container

I have a jar of a spring boot application running in a docker container. I am trying to connect it to a neo4j docker container. I get an error when I hit the url http://localhost:8080/ "Could not open Neo4j Session for transaction". But when I run the jar and neo4j container from two separate machines it connects to the neo4j db. Error only occurs if I run both containers on same host machine. Below is the log:
2019-02-25 09:02:52.413 ERROR 6 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Neo4j Session for transaction; nested exception is org.neo4j.ogm.exception.ConnectionException: Could not create driver instance] with root cause
java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.8.0_111]
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) ~[na:1.8.0_111]
at org.neo4j.driver.internal.shaded.io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:325) ~[neo4j-java-driver-1.6.3.jar!/:1.6.3-b33d284ef6f350edbfe9e8f4539cdcdb0200bd0b]
Don't bind your database to localhost.
Bind to any IP 0.0.0.0
Please read my answers here and here. It's help to people have the same problems

Failed to shut down 1 bean with phase value x within timeout of 30000:

I am using spring boot 1.5.8. I have configured 2 jms listeners. When I am trying to shut down gracefully using kill sigterm, I'm getting this exception.
13-06-2018 13:21:16 :: WARN :: defaultValueForJmsMessageId::[Thread-2] :: Failed to shut down 1 bean with phase value 2147483647 within timeout of 30000: [org.springframework.jms.config.internalJmsListenerEndpointRegistry]
13-06-2018 13:21:19 :: ERROR :: defaultValueForJmsMessageId::[AMQP Connection 10.23.219.62:5672] :: An unexpected connection driver error occured
java.lang.NoClassDefFoundError: com/rabbitmq/utility/Utility
at com.rabbitmq.client.impl.ChannelN.broadcastShutdownSignal(ChannelN.java:273)
at com.rabbitmq.client.impl.ChannelN.finishProcessShutdownSignal(ChannelN.java:291)
at com.rabbitmq.client.impl.ChannelN.access$000(ChannelN.java:50)
at com.rabbitmq.client.impl.ChannelN$1.transformReply(ChannelN.java:588)
at com.rabbitmq.client.impl.ChannelN$1.transformReply(ChannelN.java:585)
at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.handleCommand(AMQChannel.java:356)
at com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:149)
at com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:90)
at com.rabbitmq.client.impl.AMQConnection.readFrame(AMQConnection.java:634)
at com.rabbitmq.client.impl.AMQConnection.access$300(AMQConnection.java:47)
at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:572)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: com.rabbitmq.utility.Utility
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:94)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 12 common frames omitted
13-06-2018 13:21:29 :: WARN :: defaultValueForJmsMessageId::[Thread-2] :: Invocation of destroy method failed on bean with name 'rabbitConnectionFactory': java.lang.NoClassDefFoundError: com/rabbitmq/client/impl/SocketFrameHandler$1
This has happened when the application was not given enough time to stop and when restart happened immediately after the deployment of updated spring boot jar in same location.
This has not happened after giving enough time. Will update if something comes up.

mongodb on tomcat with 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)

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...)

Resources