您好,在我的项目中要连接到数据库,我正在使用contex.xml文件(我拥有所有数据库数据)和@Resource(name =“ xyz”)批注,并使用它在Servlet中创建dataSource:< / p>
@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);
}
}
但是,它每天都突然停止工作。我的意思是@Resource(name =“ sql2226123”)不提供任何数据。 dataSource == null,myConn == null;发生了什么?这次没有更改context.xml为100%良好。如果我没记错的话,最近我做了一些jdk更新。也许与它有关?我当前的Java版本是:1.8.0_81-b13。有任何想法吗?有人听说过吗?
这是我的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>