0

I am trying to create a demo, but I am not getting how it will be implemented, can any body help me to configure it.

I am using spring boot, and hibernate.

I have requirement like, first I'll get db login information via form, and then I'll save it into any file or any other source.

Now I have login detail of database, now I will allow hibernate to connect with my database and create tables, and other query will be executed.

but I am facing issue with hibernate, as project is starting it shows me error

Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

because when the project is getting started it loads hibernate to create tables and structures.

but I want to execute this task after getting database login detail from user via a form.

means I want that the project should start without login detail of database, and also hibernate do not throws any error.

What I suppose to do for resolving this problem.

Dev Sabby
  • 1,367
  • 1
  • 11
  • 17
  • what is username and password of your db ?? – Harshad_Kenjale May 27 '17 at 06:46
  • I think you are making things very hard for yourself with this approach. May I ask the reason why you don't want to set credentials in the application properties? You are working against the framework. Maybe this should not be implemented with spring? – Pär Nilsson May 27 '17 at 07:01
  • Hay y2k! I don't have username and password when I start, it will be set by the user. – Dev Sabby May 27 '17 at 07:14
  • Hi Par ! You are right I am making it more harder, I want to create tables when user provides me user name and password and url too of the database, where exactly user want to save his own data. that's why I am using this approach. – Dev Sabby May 27 '17 at 07:16
  • Can we do it by using JPA at the place of Hibernate ? – Dev Sabby May 27 '17 at 08:28
  • You must set hibernate properties programmatically. Try from [this](https://stackoverflow.com/questions/6074678/setting-properties-programmatically-in-hibernate). – Erwin May 27 '17 at 14:17

2 Answers2

1

I think what you want is multi tenancy? I have no deeper experience with this, but maybe this

https://dzone.com/articles/spring-boot-hibernate-multitenancy-implementation

would help you....

miwoe
  • 757
  • 1
  • 6
  • 17
0

I don't think it possible to configure db connection after you application started. But so I see 2 possible solutions:

  • First one is that you need lazy initialization of DataSource and EntityManager beans. So you need to configure them in hand mode and annotate them with @Lazy and call them after you receive db's login and password.But still... you will have a problem like you can connect to the second db during application execution. So, this way works but it is not recommended.
  • The second option is to don't use Hibernate and use pure JDBC connection instead. This one can't limit your connection options and give you some kind of freedom and that is why this way strongly recommended by me.
Sammers
  • 1,038
  • 2
  • 8
  • 20