Managed bean and datasource connections in ejb3.1 - jdbc

I have a bean (dbbean) that has datasource which is injected at runtime. this managed bean is used by many dao beans. I use ejb3.1 in jboss environment. I get frequent error saying closing the connection for you, please close them yourself. this dbbean is not singleton. should i need to manage the bean with scope? I have finally block that will close all database resources.
Note: I have postconstruct annotated method that will create a connection and predestroy annotated method which will close the opened connection.
thanks

Related

Spring Batch Bean Scope to convert Legacy Application

We have a legacy normal java application with below architectural flow
Facade ----> Corba remote IIOP Connection --> ServiceCommands
Above architecture we wants to convert as SpringBeans(Spring Boot) and call from MVCController and expose as RESTAPI
MVCController ---> Facade -->Corba Connection -->ServiceCommands
Please advise what is the Bean scope to be used for each layer and best architecture to be followed
Facade class Bean - Scope value="session"
CorbaConnection Class bean Scope value="session"
ServiceClass bean Scope value="singleton"
We have solved the issue by created a Corba connection Pool and make each connection from the pool is a Spring bean with scope "Session". Every session request to corba will be in Proxy mode and commit when the #service finishes. All the properties of pools has been added to maintain idle time and re-connection and return back to the pool after the service finishes. Service and Facade remains singleton bean. Thanks team for the support again to provide idea hints from your experiences.

Why creation of repository beans need a datasource bean during start-up

I was working on a Spring-data-jpa project with spring boot, I see that the creation of repository beans required a datasoure bean to be present, Why is it so?
And can a repository bean be created without datasource bean.
The purpose of a repository is to load and save data into a persistent store.
Spring Data JPA does that using JPA so it needs an EntityManager which in turn need a DataSource.
Strictly speaking the DataSource is only used once the database is actually accessed.
While you definitely nee a DataSource bean you may delay the construction of a normal DataSource by providing a wrapper which instantiates the the actual DataSource at a later point in time.
DelegatingDataSource might be of help, either as a basis class or, since you are going to change the DataSource as a template for an implementation.
See the somewhat related question https://stackoverflow.com/a/61208585/66686

Spring #PreDestroy method

I found out that #PreDestroy only work with singleton scoped bean. I was thinking what could go wrong if we use it with prototype scoped bean. Anything at all??? I dont think so. I think this is just not implemented in spring as they would have to keep the references to all the beans created. Tell me if i am wrong
Spring can only initialize/destroy beans it also controllers and basically prototype scoped beans aren't under the control of spring (after construction). It doesn't know when it is cleaned up, destroyed or what so ever. As such the #PreDestroy method isn't callable for prototype beans (as they do not have a clearly defined lifecycle like singletons or request scoped beans).
For "prototype" scoped beans, Spring does not call the #PreDestroy method.
Here is the answer from the Spring reference manual. Section 7.5.2
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-factory-scopes-prototype
In contrast to the other scopes, Spring does not manage the complete lifecycle of a
prototype bean: the container instantiates, configures, and otherwise assembles a
prototype object, and hands it to the client, with no further record of that prototype
instance.
Thus, although initialization lifecycle callback methods are called on all objects regardless of scope, in the case of prototypes, configured destruction lifecycle callbacks are not called. The client code must clean up prototype-scoped objects and release expensive resources that the prototype bean(s) are holding.
To get the Spring container to release resources held by prototype-scoped beans, try using a custom bean post-processor, which holds a reference to beans that need to be cleaned up.
The #PreDestroy annotation does not belong to Spring, it’s located in the jsr250-api library jar under javax.annotation package.
By default, Spring will not aware of the #PreDestroy annotation. To enable it, you have to either register CommonAnnotationBeanPostProcessor or specify the <context:annotation-config /> in bean XML configuration file.

Spring FactoryBean used before it is configured?

I have two FactoryBeans creating proxies for existing beans in the application context.
FactoryBeanA.getObject() is invoked as part of the singleton pre-instantiation, and it attempts to autowire the returned instance.
This autowiring needs a bean that is defined by FactoryBeanB, which has not yet been configured (had properties injected).
Can this be controlled in such a way, that I am sure both FactoryBeans are fully configured (properties injected) before any beans are attempted instantiated?
Edit:
Autowiring from FactoryBeanA objects have worked fine until I changed FactoryBeanB to require a property to be injected. After this change, I see autowiring for the A-bean try to invoke FactoryBeanB.getObject(), but this fails as properties has not yet been injected.
Problem was actually caused by my own mistake. FactoryBeanB was not configured properly as I thought.

Spring: Serialization of session scoped bean

I have a Spring bean with scope session. This bean holds a reference to another singleton bean which is not serializable. What is the best approach if I want to serialize the session scoped bean?
The same question is already asked here: Spring session-scoped beans (controllers) and references to services, in terms of serialization
The accepted answer is that:
[...]this issue is resolved in spring 3.0 by providing a proxy of non-serializable beans, which obtains an instance from the current application context
As far as I understand the speaker in the linked video it should "just work". But in my case it doesn't! When I try to serialize my session scoped bean i get a NotSerializableException.
How can I solve this problem?
You need to instruct Spring to create that proxy. In XML-based config, via <aop:scoped-proxy/> tag, in component-scan mode via annotation:
#Scope(proxyMode = ScopedProxyMode.INTERFACES)
on your controller class.
You may mark singleton reference field as transient. Then check How to execute method after deserialization and load reference from ApplicationContext.
Also, please provide stacktrace.
P.S.
It is not too good idea to use session passivation.

Resources