EJBs always have to run inside a transaction? - spring

Hy,
I have read that EJBs always have to run inside a transaction, is this true? I mean,why cant I use the ejb container to inject a dependency in some bean like spring but without transtional environment?
Thanks

By default EJB's always use transactions but you can mark the bean or more the specific each method not to use transactions using annotation #TransactionAttribute(TransactionAttributeType.NEVER).

Related

How concurrency works in SpringBoot when the default lifetime of objects is singleton

In ASP.NET the default lifecycle of objects created in IoC container is per web request. While learning SpringBoot + Webflux, I found that the default lifecycle (Bean, Repository, Service, etc) created by the IoC container is singleton. I know I can change the default scope like this:
#Scope("prototype")
but I have not yet found an example where it would be used. So if IoC creates all object as singletons, how come there are no problems with concurrency. Can someone please explain this to me.
It's a good question. Generally speaking where concurrency is an issue, for example a transaction context in the database layer, springbook uses a thread based locking mechanism. See for example 1.2. Understanding the Spring Framework Transaction Abstraction. Otherwise, yes, anything injected with CDI is a singleton unless specifically annotated otherwise. That means that you should not keep state variables in your #Component or #Service classes. As long as the methods use only parameters passed in or variable local to the method concurrency isn't an issue because ever variable is created on the stack which is unique for each call. I have seen an application work great up until the day two people log in at once.
If you have to have a class with state variables you need to do a new of that class.
Each spring-context is created with a unique thread, so where objects are created or injected that are not stateless then state information is attached to the spring-context which runs in its own thread.
See also How does Spring bean Handle concurrency

Null Pointer exception while initializing Object using "new" keyword in springboot based microservcies

What happens when I build an object using new keyword in service using springboot framework?
Does the Object created using "new" keyword is built out of the container?
No, the object created out of a "new" keyword doesn't come out of the container. There are two disadvantages to this approach. The first being that it renders the Spring framework a bit useless. Secondly, if there are #Autowired or Spring managed beans inside your "new" bean, they won't get injected. Once you do a "new", spring leaves all the subsequent hierarchical dependency injection to you.
in Spring, objects that are created using new keyword, created outside of the container. So you wouldn't get benefits like life cycle management, Security etc
You should always use Spring dependency injection to create Beans (using the #Autowired annotation is the recommended way to do this) otherwise you're losing the benefits of using the Spring framework. I would suggest reading this documentation on Beans and Dependency Injection if you haven't already

Bean Managed Transaction in a Container Managed EJB

I have a stateless EJB that using container managed transactions. Can I have a method in that EJB that can use bean managed transaction. I know I can make the whole EJB to use the bean managed transactions by using the attribute #TransactionManagement(TransactionManagementType.BEAN)
No, you cannot switch between container-managed and bean-managed transactions on a per-method basis. You either need to use entirely bean-managed transactions, or you would need to split your logic into two separate EJBs.
(I cannot think of a reason why this must be the case. I think the EJB spec could have made BEAN be just another transaction attribute type rather than having a BMT/CMT distinction. Perhaps it was felt that would make the transaction model too complicated, or there weren't sufficient use-cases to try that approach.)

What is the proper way of init of application ear under jboss

i need right at the start of my ear that some code was executed to init application before it is used. So i wonder what is the proper way of doing this, possibly via some jboss configs, or via spring?
I' using spring3 and jboss4
If you're using Spring, use a Spring way, then you won't be tied to JBoss if you ever move off it. Here are some options:
Implement InitializingBean; example
Run code inside of a bean's controller
Annotate a method in a bean with #PostConstruct; example
Any of these methods work.

spring + struts2, inject DAO into external thread

I have a web application that uses Struts2 + Spring for the resource injection, basically my DAO. Now I would like to create a thread that periodically polls the database and, if needed, send email notifications to users.
I would like to know how I can implement this in a way that this thread can use my DAO. I haven't been able to manage Spring to inject it the way I've done it. So I would like to hear suggestions and see if someone can point me to the right way.
Right now I have a thread started by a ServletContextListener, that just creates a timer and schedules an action every 5 minutes. But I can't get this action to use my DAO. I don't have any need to use this structure, I'm open to using whichever solution works.
Thanks for your help!
Edit: As axtavt suggested, I used Spring task Execution Scheduling and it works perfectly, the thing is that my task gets injected with the DAO but then I get LazyInitializationException every time I try to access a property of my fetched objects, any suggestion on how to solve that??
Perhaps the best option is to use Spring's own scheduling support, see 25. Task Execution and Scheduling (if necessary - with Quartz, see 25.6 Using the OpenSymphony Quartz Scheduler). This apporach allows you to configure your scheduled action as Spring beans, so you can wire them with other beans such as DAO.
Alternatively, you can use the following to obtain any Spring bean in web application (for example, to obtain DAO from your thread):
WebApplicationContextUtils.getWebApplicationContext(servletContext).getBean(...)

Resources