Connection problems with H2 when starting from Java and usage of dbunit - java

i want to start from my JUnit test an in memory db and fill it with data.
When i start the server via the h2.bat everything works fine for my JUnit test app.
But when i try to start the server form Java i get some sort of connection problems
(when i try to connect to the db by the webinterface i get the gui, but its functionallity is missing/wrong)
code to start the server in the #BeforeClass setup:
args = new String[] { "-tcpPort", "8082","-tcpAllowOthers"};
h2Server = Server.createTcpServer(args).start();
// i also tried the createWebServer but it didn't make any difference
code to open connection once:
String url = "jdbc:h2:tcp://localhost:8082/mem:db1";
h2Connection = DriverManager.getConnection(url,"SA","");
For some reason i either have no connection to the db but more likely there is some sort of connection but, like said i get some undefiend connection problems.
How could i try to achieve the same functionality as when running the h2.bat from cmdline?
Do i use wrong parameters on createserver?
Update 1:
I found the problem. It seems that the usage of dbUnit to inject my testdata closed my h2connection unexpectetly (still wondering why this did not happen when running the h2.bat cmdline).
I will try now to run the full in memory mode, if it works i will delete this question or write down the problems and solutions for other users.
Update 2:
It works now in full memory mode, i am not sure but for me the core problem seemed to be (which i did not expect) calling:
IDatabaseConnection dBUnitConnection = new DatabaseConnection(h2Connection);
....
dbUnitConnection.close()
also closed my h2connection.
Like said, this did not happen when i run the h2server from cmdline.

Related

Execute CommandCall/ProgramCall by JT400 with Disabled user profile

Our problem is as follows:
We have to connect to an ISeries with the JT400 and run some commands/Programs. The requirement is: Job is running in the QBatch with DISABLED user profile (because of security reasons).
We are able to make the JDBC connection, but getting the exception when executing the CommandCall/ProgramCall run() method. Our code is the following:
// This way we are able to create the JDBC connection without supplying a username/password
Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
Connection connection = DriverManager.getConnection("jdbc:db2:*local;translate binary=true;prompt=false;naming=sql;libraries=MyLib");
// This way we are trying to connect to the Iseries and to execute CommandCall
AS400 as400 = new AS400();
CommandCall commandCall = new CommandCall(as400, "ADDLIBLE LIB(MyCmdLib)");
//Similarly executing ProgramCall as
ProgramCall programCall = new ProgramCall(as400, "/QSYS.LIB/OBJLIB.LIB/MYPGM.PGM", paramlist);
programCall.run();
and the exception is
com.ibm.as400.access.AS400SecurityException: User ID is
disabled.:NONACTUSR at
com.ibm.as400.access.AS400ImplRemote.returnSecurityException(AS400ImplRemote.java:2889)
at com.ibm.as400.access.CurrentUser.getUserInfo(CurrentUser.java:87)
at
com.ibm.as400.access.AS400ImplRemote.getPassword(AS400ImplRemote.java:1585)
at
com.ibm.as400.access.AS400ImplRemote.signon(AS400ImplRemote.java:3188)
at com.ibm.as400.access.AS400.sendSignonRequest(AS400.java:3465) at
com.ibm.as400.access.AS400.promptSignon(AS400.java:3043) at
com.ibm.as400.access.AS400.signon(AS400.java:4375) at
com.ibm.as400.access.CommandCall.chooseImpl(CommandCall.java:279) at
com.ibm.as400.access.CommandCall.run(CommandCall.java:713)
We searched on the internet but couldnĀ“t find anyone with the same problem.
Thank you for reading our question.
You can't connect to the server with a disabled user.
Period. No exceptions.
It'd be a very insecure server if that was allowed.
You can create a User ID with password but set the Initial menu to *SIGNOFF for security reasons.
https://www.ibm.com/docs/en/i/7.2?topic=fields-initial-menu

Java async driver for MongoDB doesn't work on replica set when "primary" is changed

I have some troubles with the usage of java async driver (3.8.1).
I'll describe my environment:
I have a replica set (rs0) with 3 istances: let me call them A,B,C.
In my application I use Mongo and two different java driver, sync and async.
At the beginning I reached no problems but when the primary went down (and come up after some minutes changing its behavior as secondary) the part of code when I use async driver was not able to use transactions and session.
The error is the following:
com.mongodb.MongoClientException: Sessions are not supported by the MongoDB cluster to which this client is connected
at com.mongodb.async.client.MongoClientImpl$1.onResult(MongoClientImpl.java:90)
at com.mongodb.async.client.MongoClientImpl$1.onResult(MongoClientImpl.java:83)
at com.mongodb.async.client.ClientSessionHelper$2.onResult(ClientSessionHelper.java:77)
at com.mongodb.async.client.ClientSessionHelper$2.onResult(ClientSessionHelper.java:73)
at com.mongodb.internal.connection.BaseCluster$ServerSelectionRequest.onResult(BaseCluster.java:433)
at com.mongodb.internal.connection.BaseCluster.handleServerSelectionRequest(BaseCluster.java:309)
at com.mongodb.internal.connection.BaseCluster.access$800(BaseCluster.java:65)
at com.mongodb.internal.connection.BaseCluster$WaitQueueHandler.run(BaseCluster.java:482)
at java.lang.Thread.run(Unknown Source)
2019-01-21 17:02:01.906 ERROR 17560 --- [271de4498944329] org.mongodb.driver.client : Callback onResult call produced an error
java.lang.NullPointerException: null
at it.mypackage.mongo.service.ProcessoDocumentService$1.onResult(ProcessoDocumentService.java:124)
at it.mypackage.mongo.service.ProcessoDocumentService$1.onResult(ProcessoDocumentService.java:1)
at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49)
at com.mongodb.async.client.MongoClientImpl$1.onResult(MongoClientImpl.java:90)
at com.mongodb.async.client.MongoClientImpl$1.onResult(MongoClientImpl.java:83)
at com.mongodb.async.client.ClientSessionHelper$2.onResult(ClientSessionHelper.java:77)
at com.mongodb.async.client.ClientSessionHelper$2.onResult(ClientSessionHelper.java:73)
at com.mongodb.internal.connection.BaseCluster$ServerSelectionRequest.onResult(BaseCluster.java:433)
at com.mongodb.internal.connection.BaseCluster.handleServerSelectionRequest(BaseCluster.java:309)
at com.mongodb.internal.connection.BaseCluster.access$800(BaseCluster.java:65)
at com.mongodb.internal.connection.BaseCluster$WaitQueueHandler.run(BaseCluster.java:482)
at java.lang.Thread.run(Unknown Source)
Just FYI, if I comment the part of code when I use session and transactions, the error is a classic timeout, as the driver was not longer able to find replica set anymore.
Someone could help me? What I'm missing?
This is how I create my MongoClient:
connectionString = new ConnectionString("mongodb://address1:27017,address2:27018,address3:27019/?replicaSet=rs0");
MongoClientSettings settings = MongoClientSettings.builder().applyConnectionString(connectionString)
.build();
settings = settings.builder().credential(credential).build();
asyncMongoClientInstance = MongoClients.create(settings);
I found the solution by myself, as the wise man once said: "If you want an help, find it at the end of your arm".
Let's us focus on this part of code:
connectionString = new connectionString("mongodb://address1:27017,address2:27018,address3:27019/?replicaSet=rs0");
MongoClientSettings settings = MongoClientSettings.builder().applyConnectionString(connectionString)
.build();
settings = settings.builder().credential(credential).build();
asyncMongoClientInstance = MongoClients.create(settings);
I'm reallocating the settings object to another object without the connection string.
So, async library doesn't know anymore where to address the connection.
Why I did that? I wanted to dinamically add credentials to the settings. But is not possible in this way. So I created two different settings object, one with credentials and one without.
MongoClientSettings settings = MongoClientSettings.builder().applyConnectionString(connectionString).credential(credential).build();
It definitely works with this object now.

JAVA - How do I keep data stored into a internal DB (running SQL if possible)

My problem is that I don't even know what I am looking for ...
I created an app that should save data even without an internet connection and than update it into a External Server running SQL.I need to have my application to store the data into the pc itself
How do I accomplih that? What am I looking for? Derby?
in your code use
static final String DB_CONNECTION = "jdbc:mysql://127.0.0.1/dataBasename"

Ektorp CouchDB test connection

I'm working on a Java application which uses the Ektorp Framework to communicate with a CouchDB. I use the following code to create a new CouchDbInstance:
HttpClient httpClient = new StdHttpClient.Builder()
.host("localhost")
.port("5984")
.username("")
.password("");
/* no user name and password required because, its admin party */
CouchDbInstance couchDbInstance = new StdCouchDbInstance(httpClient);
Now I want to test the connection to the CouchDB before I create/modify etc. documents. There is no method for testing the connection. Do you have a tip for me?
I'm using Ektorp in my current project and we're testing this with couchDbConnector.getAllDatabases(), catching DbAccessExceptions.
This has the slight advantage over checkIfDbExists() of being totally agnostic of the actual databases configured on the CouchDB server, and not referencing any application-specific configuration for connection checking just feels a little cleaner.
You can use CouchDbConnector#getDbInfo()
I'm currently working on a project using ektorp and I am testing my database connection like this.
Step 1 -- Connect to the HttpClient as you have done
HttpClient httpClient = new StdHttpClient.Builder()
.host("localhost")
.port("5984")
.username("")
.password("");
/* no user name and password required because, its admin party */
CouchDbInstance couchDbInstance = new StdCouchDbInstance(httpClient);
Step 2 -- Create a CouchDbInstance
CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
Step 3 -- Use CouchDbConnector to connect to a specific database in your localhost CouchDB (in this example my database is called albums but you just put in the name for whatever database you are connecting to in place of albums from where you are coding)
CouchDbConnector albums = dbInstance.createConnector("albums", true);
Step 4 -- You can print off the name of the database you are connected to like this.
System.out.println(albums.getDatabaseName());
You could also create an endpoint that would print the name of the database you are connected to in the browser by creating a method of type string with
return albums.getDatabaseName();
I hope this is helpful. I used ektorp's reference documentation to learn this. It took me a while to figure out how to use ektorp like this and I am still learning!

Call to DataSource.getConnection not returning the expected connection

I have the following code:
Hashtable env1 = new Hashtable();
env1.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
log.info("Executed step 1");
env1.put(javax.naming.Context.PROVIDER_URL, "iiop://myhost.com:9301");
log.info("Executed step 2");
Context ctx = new InitialContext(env1);
DataSource ds = (DataSource)ctx.lookup("jdbc/mydatasource");
log.info("Excecuted lookup ="+ds);
conn = ds.getConnection();
I have the previous code in an standalone application that is connecting to WAS 6.1.0.3 in order to retrieve a connection from the datasource. The code is very straighforward, and I have seen the same code working in a different environment, but in this case when I call getConnection I get an exception. The datasource is WAS has the proper authentication alias set and when the connection is tested it works OK from the WAS side, but the previous code won't work.
If I change this line: conn = ds.getConnection();
to this: conn = ds.getConnection("username","password");
Then the code will work! But that's not what I want since the connections in the datasource should already have the credentials set. I was initially thinking this was a Sybase problem, but it's also happening with Oracle, so would rather say I have a problem with WAS.
If you are curious about the exceptions, for Sybase I get:
java.sql.SQLException: JZ004: User name property missing in DriverManager.getConnection(..., Properties).DSRA0010E: SQL State = JZ004, Error Code = 0
at com.sybase.jdbc2.jdbc.ErrorMessage.raiseError(ErrorMessage.java:569)
at com.sybase.jdbc2.tds.LoginToken.<init>(LoginToken.java:128)
at com.sybase.jdbc2.tds.Tds.doLogin(Tds.java:506)
at com.sybase.jdbc2.tds.Tds.login(Tds.java:449)
at com.sybase.jdbc2.jdbc.SybConnection.tryLogin(SybConnection.java:254)
at com.sybase.jdbc2.jdbc.SybConnection.regularConnect(SybConnection.java:230)
at com.sybase.jdbc2.jdbc.SybConnection.<init>(SybConnection.java:200)
at com.sybase.jdbc2.jdbc.SybPooledConnection.<init>(SybPooledConnection.java:72)
at com.sybase.jdbc2.jdbc.SybConnectionPoolDataSource.createConnection(SybConnectionPoolDataSource.java:138)
at com.sybase.jdbc2.jdbc.SybDriver.connect(SybDriver.java:485)
at com.sybase.jdbc2.jdbc.SybDriver.connect(SybDriver.java:517)
at com.sybase.jdbc2.jdbc.SybDataSource.getConnection(SybDataSource.java:227)
at com.sybase.jdbc2.jdbc.SybConnectionPoolDataSource.getPooledConnection(SybConnectionPoolDataSource.java:74)
at com.ibm.ws.rsadapter.spi.InternalGenericDataStoreHelper$1.run(InternalGenericDataStoreHelper.java:897)
at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:118)
at com.ibm.ws.rsadapter.spi.InternalGenericDataStoreHelper.getPooledConnection(InternalGenericDataStoreHelper.java:892)
at com.ibm.ws.rsadapter.spi.WSRdbDataSource.getPooledConnection(WSRdbDataSource.java:1181)
at com.ibm.ws.rsadapter.spi.WSManagedConnectionFactoryImpl.createManagedConnection(WSManagedConnectionFactoryImpl.java:1047)
at com.ibm.ws.rsadapter.spi.WSDefaultConnectionManagerImpl.allocateConnection(WSDefaultConnectionManagerImpl.java:81)
at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:431)
at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:400)
And for Oracle I get this one:
java.sql.SQLException: invalid arguments in callDSRA0010E: SQL State = null, Error Code = 17,433
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:236)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:420)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at oracle.jdbc.pool.OracleDataSource.getPhysicalConnection(OracleDataSource.java:297)
at oracle.jdbc.pool.OracleDataSource.getConnection(OracleDataSource.java:221)
at oracle.jdbc.pool.OracleConnectionPoolDataSource.getPhysicalConnection(OracleConnectionPoolDataSource.java:157)
at oracle.jdbc.pool.OracleConnectionPoolDataSource.getPooledConnection(OracleConnectionPoolDataSource.java:94)
at oracle.jdbc.pool.OracleConnectionPoolDataSource.getPooledConnection(OracleConnectionPoolDataSource.java:75)
at com.ibm.ws.rsadapter.spi.InternalGenericDataStoreHelper$1.run(InternalGenericDataStoreHelper.java:897)
at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:118)
at com.ibm.ws.rsadapter.spi.InternalGenericDataStoreHelper.getPooledConnection(InternalGenericDataStoreHelper.java:892)
at com.ibm.ws.rsadapter.spi.WSRdbDataSource.getPooledConnection(WSRdbDataSource.java:1181)
at com.ibm.ws.rsadapter.spi.WSManagedConnectionFactoryImpl.createManagedConnection(WSManagedConnectionFactoryImpl.java:1047)
at com.ibm.ws.rsadapter.spi.WSDefaultConnectionManagerImpl.allocateConnection(WSDefaultConnectionManagerImpl.java:81)
at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:431)
at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:400)
In both cases I won't the exception if I pass the credentials to the getConnection method
Thanks for your advice.
Short answer: external clients don't get to use the authentication alias data
Longer Answer:
From the WAS J2C connection factory documentation:
The alias that you configure for component-managed authentication does not apply to all clients that must access the secured resource. External Java clients with Java Naming and Directory Interface (JNDI) access can look up a Java 2 Connector (J2C) resource such as a data source or Java Message Service (JMS) queue. However, they are not permitted to take advantage of the component-managed authentication alias defined on the resource. This alias is the default value that is used when the getConnection() method does not specify any authentication data, like user and password, or a value for ConnectionSpec. If an external client needs to get a connection, it must assume responsibility for the authentication by passing it through arguments on the getConnection() call.
It's been a long time since I've done anything with WebSFEAR^H^H^H^Hphere, but it looks to me that you have a configuration problem. There was a special screen where you'd create credentials (user/pass) and later you'd apply those credentials to the created data source. It looks like that your configured data source hasn't got credentials applied.
Even after defining the user/password values as custom properties I found that the connections for Oracle weren't working. After many days, I just found that the development server is running an old WAS 6.1 version, the problem I'm having was fixed in WAS 6.1.0.5: PK32838: J2CA0046E WHEN USING USING CUSTOM PROP PASSWORD ON DATASOURECE
I tried my code in a different WAS server with an updated WAS fix pack level and... it worked without introducing a single change in the code or in the configuration. So the solution is to upgrade the WAS server.
Thanks.

Categories

Resources