How to use inmemory data base for Junits in Play 2.2 - frameworks

I am working on play 2.2 for my application. In my application.conf, I have specified the connection properties as below.
db.testdb.driver=org.h2.Driver
db.testdb.url="jdbc:h2:mem:play"
db.testdb.user=xxxxx
db.testdb.password=xxxx
And When I try to access the h2 db in my Jnuit , I am getting connection "testdb" undefined error.
Junit:
FakeApplication app = Helpers.fakeApplication(Helpers.inMemoryDatabase("testdb"));
Helpers.start(app);
Please help me to connect h2 db from my Junit.
Thanks in adv.

Try to use default setting for h2 db:
db.default.driver=org.h2.Driver#
db.default.url="jdbc:h2:mem:play#"
db.default.user=sa
db.default.password=""

Related

MongoDB "no SNI name sent" error from heroku java application

I followed this tutorial to deploy a sample application to Heroku. I just added the below method in MyResource class and returned the result from it instead of "Hello World" from getIt() method. I'm connecting to an atlas free tier cluster:
static String getMessage() {
MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://<USER>:<PASSWORD>#cluster0-shard-00-00-2lbue.mongodb.net:27017,cluster0-shard-00-01-2lbue.mongodb.net:27017,cluster0-shard-00-02-2lbue.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin"));
DB database = mongoClient.getDB("mastery");
DBCollection collection = database.getCollection("summary");
DBObject query = new BasicDBObject("_id", new ObjectId("5c563fa2645d6b444c018dcb"));
DBCursor cursor = collection.find(query);
return (String)cursor.one().get("message");
}
This is the driver I'm using:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.9.1</version>
</dependency>
This is my import:
import com.mongodb.*;
The application works fine from my local system. But I face the below error when I deploy the application to Heroku and hit the service:
INFO: Exception in monitor thread while connecting to server cluster0-shard-00-01-2lbue.mongodb.net:27017
com.mongodb.MongoCommandException: Command failed with error 8000 (AtlasError): 'no SNI name sent, make sure using a MongoDB 3.4+ driver/shell.' on server cluster0-shard-00-01-2lbue.mongodb.net:27017. The full response is { "ok" : 0, "errmsg" : "no SNI name sent, make sure using a MongoDB 3.4+ driver/shell.", "code" : 8000, "codeName" : "AtlasError" }
at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:179)
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:299)
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
at com.mongodb.internal.connection.CommandHelper.sendAndReceive(CommandHelper.java:83)
at com.mongodb.internal.connection.CommandHelper.executeCommand(CommandHelper.java:33)
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:106)
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:63)
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127)
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
at java.lang.Thread.run(Thread.java:748)
What is this SNI name? I can understand that the drivers are able to pick it from my machine, but not from Heroku machine. But I'm clueless on how to go about solving this! Is there a way to configure Heroku to reveal the SNI name when the driver asks for it? Can we get this value manually from somewhere in Heroku and directly feed it to the MongoDB drivers? Any help is appreciated.
EDIT:
It turned out that the client mentions the SNI name of the server it wishes to connect to as part of TLS security. And there seems to be a way to manually indicate the name in python driver. Is there any way to do this from java? Still puzzled why this is not an issue when running the app locally.
The code I was using to connect to the cluster turned out to be wrong. I followed the directions from the docs and it mentioned this:
To connect to an Atlas M0 (Free Tier) cluster, you must use Java
version 8 or greater and use a Java driver version that supports
MongoDB 3.4.
So I changed the java version to 1.8 in system.properties file:
java.runtime.version=1.8
It was earlier set to 1.7. I was also getting a deprecation warning on one of the methods I had used. So I again followed the docs to use the latest code and it worked like a charm.
The real takeaway here is to refer to the official docs everytime :)

Flyway cannot connect to db after Heroku Postgres upgrade

I am upgrading my heroku database from a hobby dev to Standard 0 (using the official instructions https://devcenter.heroku.com/articles/upgrading-heroku-postgres-databases#upgrade-with-pg-copy-default).
All went well, until I promoted the new database and restarted the app. I then get the following error:
o.s.boot.SpringApplication : Application startup failed
...
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource
...
Caused by: org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource
...
Caused by: org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for host "54.xxx.xx.xxx", user "u94bf9vxxxxxx", database "d2mqk0b6xxxxxx", SSL off
...
If I swap back to the old database again, everything works again. The only thing that I am changing is the promoted database.
Is there a difference between connecting to hobby and standard databases that I need to be aware of?
The relevant part of my application.yml looks as follows:
spring:
datasource:
driverClassName: org.postgresql.Driver
url: ${JDBC_DATABASE_URL}
username: ${JDBC_DATABASE_USERNAME}
password: ${JDBC_DATABASE_PASSWORD}
flyway:
enabled: true
locations: classpath:db/migrations
Any suggestions on how I can debug this would be very welcome too.
Looks like you aren't connecting with SSL where it is required by Heroku PostgreSQL installs.
See Herokus documentation on SSL for PostgreSQL.
See also Herokus documentation for enabling SSL on JDBC connections.
You will need to add something like &ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory to your JDBC URL.

What version of Salat/Casbah should I use to connect to Mongo 3.x server?

I am using https://github.com/salat/salat. But it seems that salat is using Casbah 2.7. Our mongo server is 3.0.6. But we are not able to connect and always got this error.
Configuration error: Configuration error[Access denied to MongoDB database: [mg_prod] with user: [mydbuser]]
at play.api.Configuration$.play$api$Configuration$$configError(Configuration.scala:94)
at play.api.Configuration.reportError(Configuration.scala:743)
at se.radley.plugin.salat.SalatPlugin$MongoSource.connection(SalatPlugin.scala:36)
at se.radley.plugin.salat.SalatPlugin$$anonfun$onStart$1.apply(SalatPlugin.scala:136)
at se.radley.plugin.salat.SalatPlugin$$anonfun$onStart$1.apply(SalatPlugin.scala:131)
Can you please give me some idea how should I fix this issue?
As a workaround you can add
"org.mongodb" %% "casbah" % "2.8.2"
to your build.sbt file and the error should be gone.

How to configure hello-slick-3.0 app for mysql (async)

I'm trying to update the hello-slick-3.0 application downloaded using typesafe activator to use mysql rather than the h2 in-mem database. I've changed all the references to h2 to import import slick.driver.MySQLDriver.api._, but I don't know how to force the correct version of this driver at runtime. I'm getting this error at runtime:
background log: info: Running HelloSlick
background log: error: Exception in thread "main" java.sql.SQLException: No suitable driver
I wish to start using the async mysql driver with scala/slick.
You need to use standard MySQL JDBC driver, for example:
libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.35"
Slick 3 implements "asynchrony" itself using Reactive Streams.

How can I connect vertica with JDBC?

I want to connect vertica with JDBC. But I got errors.
Here is my code :
....
Class.forName("com.vertica.jdbc.Driver");
....
connection= DriverManager.getConnection
(
"jdbc:vertica://192.168.2.116:5433/schema", "dbadmin", "pass123"
);
But I got this error(if I open the netbeans database section I got same error message. But I connect to vertica with client(Dbeaver)) :
ex = (java.sql.SQLException) java.sql.SQLException: [Vertica]No enum const class com.vertica.dsi.dataengine.utilities.MetadataSourceColumnTag.COLUMN_SİZE
How can I fix this?
So if you need jdbc client for vertica in netbeans or intellij use this vertica jdbc driver. It's the one that worked for me. (taken from dbvisuzlizer).
i think it is because of your locale. in this case turkish i guess.
COLUMN_SİZE has upper case i -> İ
it is verticas fault to use toUpper digressivly.
Vertica's connect string uses databasename, not schema name after the host:port. See the doc for details:
https://my.vertica.com/docs/CE/6.0.1/HTML/index.htm#1395.htm
Connection conn = DriverManager.getConnection(
"jdbc:vertica://VerticaHost:portNumber/databaseName",
"username", "password");
By default, users have a search path of "$user, public, v_catalog, v_monitor and v_internal", therefore, you can create and use a matching username to connect directly to the desired SCHEMA.
Its about 32 bit - 64 bit issue I think, because it is working on 32 bit windows I cant understand
make sure the connector (vertica-jdbc-xxxx.jar) is in the JDK\jre\lib\ext folder