jee:jndi-lookup use to connect to database - java

I am working on spring application. I need to connect my application to database to get some records. I am failing to connect to database. It says "Table or view doesn't exist". I have the table and i can run the query in TOAD to view the records.
In dataSource-config.xml, i have the below line.
<jee:jndi-lookup id="cifDataSource" jndi-name="java:jdbc.datasource.CIFDataSource" resource-ref="true" />
What is the use of the above line. Do i need to create any object related to "CIFDataSource" . Please advice.

There appears to be a DataSource configured in your Java EE application server.
You need to find this configuration and point it at the database that contains your tables.

Related

Connecting to data source without specifying database name

I'm using spring boot and database mongo. Our database is placed on AWS cluster. Inside data source we have couples databases(AccountsDB, SubscriptionsDB etc). I did realization, when in Spring boot I connected to 2 databases using disable autoconfiguration in application.properties file and connect to mongoDB twice: ones, to database AccountsDB(
mongodb://${MONGO_DB_USERNAME}:${MONGO_DB_PASSWORD}#${MONGODB_HOST}:${MONGODB_PORT}/{AccountsDB}?replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false
), and second connection to SubscriptionDB(
mongodb://${MONGO_DB_USERNAME}:${MONGO_DB_PASSWORD}#${MONGODB_HOST}:${MONGODB_PORT}/{SubscriptionDB}?replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false
).
But my manager unhappy with this realization... There is any way to connect to data source without clarification of data base name? I couldn't find any solution for this, if we are not clarify its not showing any data. Thank you for any replying
I tried to connect without clarification, and it doesn't display any data
mongodb://${MONGO_DB_USERNAME}:${MONGO_DB_PASSWORD}#${MONGODB_HOST}:${MONGODB_PORT}/?replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false
Might be any way to connect to datasource and after using SpringJPA make connection?

How to create a database if it does not exist with springboot/JPA and SQL Server?

I am surprised I haven't found an SO question that answers this. I am trying to connect a springboot/JPA application to an SQL Server on my local machine. I have the application setup so that it can connect to a database if it it exists, but if I change the JDBC URL to create the database if it doesn't exist then it fails. Here is what the properties look like when it fails.
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=testing;createDatabaseIfNotExist=true;
spring.datasource.username=hello
spring.datasource.password=Hello1234
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.database-platform=org.hibernate.dialect.SQLServer2008Dialect
spring.jpa.show-sql=true
Here is a snippet of the error I receive when starting the app:
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user
'hello'. ClientConnectionId:971a3369-258b-4713-bddc-cda559b9fe94 at
com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:262)
~[mssql-jdbc-8.4.0.jre11.jar:na] at
com.microsoft.sqlserver.jdbc.TDSTokenHandle
If anybody has any thoughts as to how I can change this so the database is created if it does not exist I would be very thankful. Thanks in advnace.
I don't think a database can be created using JPA.
It has to be created manually or in some other ways, but JPA won't do that for you.
And it would be a bad practice as well to create the database using the application itself, and the use of same credentials.
Yes, definitely you can auto-create a database with JPA for that
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=testing;
createDatabaseIfNotExist=true;
line should be changed to:
spring.datasource.url=jdbc:sqlserver://localhost:1433
/testing?createDatabaseIfNotExist=true
In practice your application should never create your database so its not really a problem most of the time(Outside small databases like sqlite3). Different databases would handle this situation differently as well.
In your case I do not see this as a valid jdbc parameter in the documentation.
I would recommend creating the database in advance with a privileged user separate from your application user.

How to create table postgresql Heroku?

I have database postgresql in Heroku and i have local app. My entity is "Product".
When i wanna open my application in spring boot i get ERROR: relation "product" does not exist.
I dont have any idea how create new table "product" in postgresql.
In mysql everythink is good
You can with do it manually by running heroku pg:psql and then running the appropriate SQL statements. Or you can use a migration framework in your app.
For more information on running database migrations see the Heroku docs.

Spring-boot application, H2 embedded database - but tables disappear when the application closes

My database.properties file is:
datasource.driver=org.h2.Driver
datasource.url=jdbc:h2:file:./test_database/comixed_db;create=true
datasource.username=sa
datasource.password=
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=true
hibernate.batch.size=20
hibernate.current.session.context.class=org.springframework.orm.hibernate5.SpringSessionContext
hibernate.generate_statistics=false
hibernate.enable_lazy_load_no_trans=false
When my application starts up I see I am able to see the tables by using the h2.bat tool and peeking at the database from a web browser. However, when I shut down the application and then go back to the database with the h2.bat tool the tables are all gone!
AM I doing something incorrectly with my hibernate configuration? I am not using create-drop but update since this code is currently in flux and I'd like the tables to be adjusted as changes occur. But that doesn't seem to be the issue since it's at app shutdown that the tables keep going away.
Any help would be appreciated.
If you want spring boot to catch your hibernate properties you should prefix them with spring.jpa, so:
spring.jpa.hibernate.ddl-auto=update
Otherwise, and that is the case in my opinion, spring will use the default create-drop options as it is dealing with an H2 in-memory database.
By adding the following line to applications.properties:
spring.jpa.hibernate.ddl-auto=update
Spring-boot stopped dropping tables when the application exits.

Fallback Database with Hibernate

do someone of you know a way to have 2 Databases running parallel?
We are using Hibernate 4 and as a main database Postgres 9.3 - this db is hosted on another machine then the application - if the database is down we still have to save some stuff.
So first intention was to write it into a csv, but I'm not a friend of writing stuff into an unordered file. So I want just to use a fallback Database (thinking of H2 Database). Does someone has experience with such a construct?
We are also using Spring 4 - I would just set up another datasource + sessionfactory + transactionmanager - and add the name at the #Transactional method to use the right manager. any other ideas?
Thank you!!
You can extend the Spring AbstractRoutingDataSource and configure two actual data sources:
a primary PostgreSQL data source
a secondary H2 data source
The application logic will see only one data source, which is the router which will decide which data source is going to switch to on demand.
When the primary data source is down you need to instruct the router to pick the fall-back H2 one.

Categories

Resources