-
1. Re: Resource Injection in ServletContextListener
prasanth_jboss Jul 31, 2015 11:25 AM (in response to prasanth_jboss)Minor correction the variable DataSource is declared as static (private static DataSource dataSource).
If I remove static from variable declaration then the injection is working.
-
2. Re: Resource Injection in ServletContextListener
prasanth_jboss Jul 31, 2015 1:44 PM (in response to prasanth_jboss)Looks like wildfly has stopped support for injecting into static variables. The work around is to create a non static variable for the injection to work and then in contextInitialized function set the static variable with the non static variable reference.
@Resource(name="DocServer") private DataSource docDataSourceInjection;
private static DataSource docDataSource;
public void contextInitialized(ServletContextEvent event){
docDataSource = docDataSourceInjection;
}
Another approach is to make the class singleton (if that works for your design) and store the instance created by the container.
-
3. Re: Resource Injection in ServletContextListener
jaikiran Jul 31, 2015 2:49 PM (in response to prasanth_jboss)The spec states that the resource injection is only supported in non-static fields. At some point, JBoss AS7 used to support injection in static fields but as far as I remember that was just accidental and not intentional and that was fixed in a later release as you notice now.
-
4. Re: Resource Injection in ServletContextListener
prasanth_jboss Jul 31, 2015 3:01 PM (in response to jaikiran)Good to know. Thank you JaiKiran.
-
5. Re: Resource Injection in ServletContextListener
prasanth_jboss Jul 31, 2015 3:06 PM (in response to jaikiran)It would be nice if Wildfly could through an error/warning message as to why the injection is not done, when it sees a static variable.
-
6. Re: Resource Injection in ServletContextListener
jameslivingston Aug 2, 2015 7:29 PM (in response to prasanth_jboss)Probably a better question is why you want to store it in a static variable, it probably means something is wrong in your design since the datasource may only be valid between the servlet instance's creating and destruction.
-
7. Re: Resource Injection in ServletContextListener
prasanth_jboss Aug 3, 2015 12:59 PM (in response to jameslivingston)This class handles all database connection requests for the project (has a static getConnection method). Rather than trying to get a datasource from JNDI every time it gets a connection request I am storing the datasource from the initial resource injection. I am open for any better solutions.
Note: As mentioned above as a solution, I did make the class singleton. Saving the instance created by the container to call the context initialized method.
-
8. Re: Resource Injection in ServletContextListener
jameslivingston Aug 3, 2015 8:09 PM (in response to prasanth_jboss)The correct solution is to @Resource inject the DataSource object into anything that needs it, and simply use ds.getConnection() rather than YourUtility.getConnection().
Using a static helper may appear easier, but you shouldn't do it since it can cause all sorts of issues like this.
-
9. Re: Resource Injection in ServletContextListener
prasanth_jboss Aug 4, 2015 10:11 AM (in response to jameslivingston)@Resource injection can't be used in business classes which would require the database connection. It is limited to Servlets, Filters & Listeners. Segregating the code that handles the database connections would make it easy to change how we get connections. We used to have a direct JNDI lookup for datasource if we had that code in every class that needs a connection that would be so much duplication of code. Later on the lookup was replaced with @Resource injection. In the future if we have to change this to some thing else there will be one class to modify rather than modifying every class that uses a database connection.
I didn't realize the spec doesn't support static variable injection as JBoss was supporting it. With a singleton class you can avoid static variables and at the same time segregate the code that gets you the database connection.