0

Hello in my project to connect to the DB I was using contex.xml file (where I had all DB data) and @Resource(name = "xyz") annotation to and use it to create dataSource in my Servlet:

@WebServlet(name = "BookControllerServlet", urlPatterns = {"/BookControllerServlet"})
public class BookControllerServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

private BookDbUtil bookDbUtil;

@Resource(name = "sql2226123")
private DataSource dataSource;

// init method will be called by the app server when this servlet is loaded or initialized
@Override // we inherit it from GenericServlet
public void init() throws ServletException {
    super.init(); //To change body of generated methods, choose Tools | Templates.

    // create our book db util ... and pass in the connection pool / datasource
    try {
        bookDbUtil = new BookDbUtil(dataSource); // bookDbUtil is a data member that we've defined
    } // dataSource is resource injection item our connection pool and we're passing it right here
    catch (Exception exc) {
        throw new ServletException(exc);
    }
}

However, from day to day it suddenly stopped working. I mean @Resource(name = "sql2226123") does not provide any data. dataSource == null, myConn == null; What happened? context.xml is 100% good was not changed by this time. I had some jdk update recently if I remember correctly. Maybe it has something to do with it? My current java version is: 1.8.0_81-b13. Any ideas? Did anyone hear something about it?

Here is my context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Context path="jadbc/ParkingSystem">
    <Resource name="sql2226123" 
        auth="Container" type="javax.sql.DataSource"
        maxActive="20" maxIdle="5" maxWait="10000"
        username="sql2226123" password="xxxxxxxx" 
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://sql2.freemysqlhosting.net:3306/sql2226123"/>
</Context>

I also manage to get following error: enter image description here

Luke_Nuke
  • 461
  • 2
  • 6
  • 23
  • Does the database table(s) have any data or rows? Is there any error in the web server log files (the code is referring to a servlet)? – prasad_ Aug 29 '18 at 03:25
  • Unfortunately, there is no error at all. No info, just white page and with the debugger I found that my @Resource() annotation does not work dataSource value is just null. I have a few backup builds I've made during making the project, and each of them was checked before I saved it as a backup. All of them have the same problem :( – Luke_Nuke Aug 29 '18 at 07:24
  • Please edit the post and show the contents/configuration in the _context.xml_. – prasad_ Aug 29 '18 at 07:42
  • I've added my context.xml and also managed to get 500 error pls see screenshot. – Luke_Nuke Aug 29 '18 at 18:50
  • `NameNotFoundException` is a common issue faced with configuring a JDBC `DataSource` object within a web server. I googled a bit and found some links with similar issues and solutions. I am hoping you will try the same and find something useful to your specific case. I can't for sure say what is wrong by looking at the error message or the configuration file. _Continued in the next comment..._ – prasad_ Aug 30 '18 at 01:34
  • Here is a link with similar issue: [https://stackoverflow.com/questions/49626981/javax-naming-namenotfoundexception-name-jdbc-mydb-is-not-bound-in-this-contex](https://stackoverflow.com/questions/49626981/javax-naming-namenotfoundexception-name-jdbc-mydb-is-not-bound-in-this-contex) . You can try searching with the text "javax.naming.NameNotFoundException jdbc/datasource name is not bound in this context" and look for more information and a possible solution. – prasad_ Aug 30 '18 at 01:34
  • Is the MySQL database server up and running? – prasad_ Aug 30 '18 at 01:42
  • Ill check solution form other question you showed me. MySQL is 100$ running I logged as admin (with the same data I use in context.xml) and add/remove records. All seems to be working. – Luke_Nuke Aug 30 '18 at 07:26
  • Instead of using @Resource injection for `DataSource` try using `javax.naming.InitialContext` and try to access the resource; try this in the `init` method: `InitialContext cxt = new InitialContext();` `DataSource ds = (DataSource) cxt.lookup("java:/comp/env/sql2226123");` `Connection conn = ds.getConnection();` . – prasad_ Aug 31 '18 at 13:05

0 Answers0