We have a Spring Boot 2.6 application with a PostgreSQL database. We use all the default database mechanisms (connection is defined in config via spring.datasource.* etc) - nothing special here.
Now we are looking for a hook point after the database connection was set up. We need to perform some checks before rest of the beans are loaded.
1. Spring start
2. Connect database
#. { hook point - to perform some db things }
3. Startup of other beans (which also rely on database)
Which hook point / Spring event / what else can we use to perform our checks?
Related
We have an application that uses several data sources. A DB underlying one of those data sources is down at the moment: IOError. Network adapter couldn't establish the connection & Socket read timed out.
Is there an annotation (or other means) of configuring Spring Boot such that it bypasses the culprit data source and still starts up: the DB is not essential in current development work. spring.datasource.continue-on-error=true doesn't seem to work. This is Spring 2.2.2.RELEASE.
using multiple datasource, so when your apps fail at start up your apps still work, i mean using memory db / sqlite to handle fail at connection error...
I'm confused about how to connect Spring dynamically to multiple databases, depending on the request?
For Example :
1. In admin site, register new member.
2. Database admin creates a new database for this new member.
3. Spring boot .jar, when in run mode, just adds some configuration lines to the external file, this service can connect to the new added database.
member 1 has url 1 => www.example.com/member-1 connect to db_member_1
member 2 has url 2 => www.example.com/member-2 connect to db_member_2
... etc
So I mean, I don't need to update my source code, but just add some lines in my external configuration file,so this spring boot .jar can connect to the new database.
I have tried connect two databases in 1 spring boot but I don't know how to make it dynamically without unloading my spring boot.
I use JPA to connect my master database and use JdbcTemplate to connect my transaction database.
Thanks in advance. :)
Regards,
Peter Susanto
We have never used the WS-Transaction policy before in Websphere, therefore I am in the process of developing a prototype to evaluate it.
This is how the prototype is structured.
I have developed two applications, APP_A and APP_B using eclipse.
APP_A contains two JAXWS web services, service_X and service_Y
service_X accepts minimal data, writes it to a table on an oracle database and then returns the primary key in the response
service_y accepts the primary key returned from service_X and some other minimal data, writes it to a different table on the same oracle database and then returns the primary key in the response
APP_B contains one JAXWS web service, service_Z
service_z accepts all data required to post a request to service_x and service_y, and does so returning both primary keys returned from service_x and service_y in its response
both APP_A and APP_B are deployed to Websphere 8.5.5.9, and have been tested successfully.
Then via the Websphere admin console, I created a new application policy set and included the ws-transaction policy. I then attached this policy to the provider policies within APP_A and the client policies within APP_B, and then restarted Websphere.
I then submitted a request to APP_B, from the log files I can see that the request is being successfully received by service_z in APP_B, but when it attempts to post a request to service_x in APP_A the following error is reported:
[02/06/16 11:53:43:040 BST] 00000076 AxisEngine E org.apache.axis2.engine.AxisEngine receive WTRN0127E: Operation blocked by policy type configuration.
I have followed several online articles from IBM developer works that all imply everything should work by just applying the policies via the admin console, but unfortunately I still encounter this error.
I suspect that I may need to include something within the header of the service call, but I am not sure what this is or how to achieve it.
One of the articles suggested using the IBM Rational Application Developer tool, exporting the policies from the admin console and then importing them into the project prior to deployment. I tried this using IBM RAD 9.11 as per the instructions, but it still does not resolve the issues.
Any help/advice would be appreciated, thanks in advance for your help.
I think that you have specified mandatory WS-AT Policy but there is no JTA transaction on the thread. Presumably you want to start a transaction in APP_B and use WS-AT to propagate that transaction on the requests to service_X and service_Y so that all work is coordinated under a single transaction. If service_Z is a POJO you can use UserTransaction to begin/commit the transaction or you could just use a single request to an EJB with CMT to wrap the multiple web service requests to the backend services. NOTE the PolicySet won't automatically start the JTA transaction for you - the easiest way to think of WS-AT is that it is for webservices what JTS/OTS is for CORBA/IIOP (ie EJB requests) - so transaction lifecycle is via JTA in a J2EE app server and transaction propagation and 2PC coordination is via WS-AT for webservices. The way to think about the WS-AT PolicySet config is that on the requester-side it determines whether an existing JTA transaction on the thread is propagated on the request (and whether it should police that a transaction must be present) - on the provider-side it determines whether it should import and execute under a WS-AT transaction context if present and whether it is valid to invoke the sevice if no tran context is present.
Hope this helps.
Not sure if you are referring to the spring class WebSphereUowTransactionManager or WebSphere's UOWManager. If you want a single transaction that all the web services execute under you'll need to start that in service_Z - since we seem to be talking POJOs/mock servlets then in standard J2EE you'd just lookup the UserTransaction interface in jndi and call begin/commit (if you're using Spring then their interfaces will do the equivalent thing under the covers - within the app server it's simply a JTA transaction associated with the current thread that matters).
With the PolicySets configured for mandatory WS-AT that transaction will be propagated on the JAX-WS requests and the target services will execute under that transaction so you don't need (or want) any explicit interaction with the JTA transaction manager (vanilla JTA or Spring) in those services.
On the other hand if you're just hoping that the PolicySet config will create separate transactions in service_X and service_Y ie like having Required/RequiresNew for CMT EJBs so you don't need explicit code to scope the transactions then I'm afraid there isn't a WS-AT policy set equivalent. It's basically JTA for transaction scoping and WS-AT for propagation/2PC with policyset config allowing some configurability over the propagation.
In my Spring Application, at the time of context Initialization. The DB is not available, it will come up after some time (Due to DB redundancy). In this scenario, my spring application initialization should be delayed or the application should do retry for DB connectivity. How to achieve the same via Spring.
Srirama.
I'd suggest investigating the ApplicationContextInitializer. It is meant to be used for setting up your context before most of the magic of spring initialization happens.
I'm not sure if it is designed for your use case, but no beans are initialized when the initialize method is called during startup.
The example provided in the link deals with properties, but I see no reason why you should not create your own manually created connection and wait for it's readiness.
Thanks for the reply.
My application is initialized by means of C3P0. Here C3P0, is trying to reconnect to DB only for 30 times (the default configuration for acquireRetryAttempts) and after that it is saying failed to create the application beans.
Changed the configuration for acquireRetryAttempts to -1, so that C3P0 is retrying indefinitely till the DB connection is successful. Basically my app. initialization should be delayed till the DB comes up.
Srirama.
I have been researching Spring Data Rest especially for cassandra and one of the questions my coworkers and I had was when does Spring Data connect to the database. We don't always want a rest controller to connect to the database so when does spring establish a connection if say we had a class extend the CRUDRepository? Does it connect to the database during the start of application itself? Is that something we can control?
For example, I implemented this example on Spring's website:
https://spring.io/guides/gs/accessing-data-rest/
At what point in the code does spring connect to the database?
Spring will connect to the DB as soon as the Datasource get initialized. Basically, Spring contexts will become alive somehow (Web listeners, manually calling them) and start creating beans. As soon as it reaches the Datasource, connection will be made and the connection pool will be populated.
Of course the above is based on a normal out of the box configuration and everything can be setup up to your taste.
So unless, you decide to control the connections yourself, DB connections will be sitting there waiting to be used.
Disagree with the above answer.
As part of research i initiated the datasource using a bean configuration and then changed my database password(not in my spring application but the real db username password)
The connection stays for a while and then in some point of time (maybe idle time) it stops working and throws credential exception.
This is enough to say the JPA does not keep the connection sitting and waiting to be used but uses some mechanism to occupy/release the db connection as per the need.