DataSource不能为null Java

时间:2018-03-10 11:30:49

标签: java

我的类ConnectionProvider返回一个null DataSource对象。而且我不知道为什么。在其他计划的早期,它始终有效。

ConectionProvider.java

public class ConnectionProvider {

    private static DataSource dataSource;

    public static Connection getConnection() throws SQLException {
        return getDataSource().getConnection();
    }

    public static DataSource getDataSource() {
        if (dataSource == null) {
            try {
                Context initialContext = new InitialContext();
                Context envContext = (Context) initialContext
                        .lookup("java:comp/env");
                dataSource = (DataSource) envContext.lookup("jdbc/znalezisko");
            } catch (NamingException e) {
                e.printStackTrace();
            }
        }

        return dataSource;
    }
}

UserDAOImpl.java(返回null DataSource):

public class UserDAOImpl implements UserDAO {

    private static final String CREATE_USER = "INSERT INTO user(username, email, password, is_active) VALUES(:username, :email, :password, :active);";

    private NamedParameterJdbcTemplate template;

    public UserDAOImpl() {
        template = new NamedParameterJdbcTemplate(ConnectionProvider.getDataSource());
    }

    @Override
    public User create(User user) {
        User resultUser = new User(user);
        KeyHolder holder = new GeneratedKeyHolder();
        SqlParameterSource paramSource = new BeanPropertySqlParameterSource(user);
        int update = template.update(CREATE_USER, paramSource, holder);
        if(update > 0) {
            resultUser.setId((Long)holder.getKey());
            setPrivigiles(resultUser);
        }
        return resultUser;
    }

    private void setPrivigiles(User user) {
        final String userRoleQuery = "INSERT INTO user_role(username) VALUES(:username)";
        SqlParameterSource paramSource = new BeanPropertySqlParameterSource(user);
        template.update(userRoleQuery, paramSource);
    }

这是contex.xml:

<Context>
    <Resource
        name="jdbc/znalezisko"
        auth="Container"
        type="javax.sql.DataSource"
        initialSize="10"
        maxTotal="100"
        maxIdle="30"
        maxWaitMillis="10000"
        username="****"
        password="****"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/znalezisko" />

    <Realm
        className="org.apache.catalina.realm.DataSourceRealm"
        dataSourceName="jdbc/znalezisko"
        userTable="user"
        userNameCol="username"
        userCredCol="password"
        userRoleTable="user_role"
        roleNameCol="role_name"
        localDataSource="true" />
</Context>

这是控制台中的所有错误:

  

javax.naming.NameNotFoundException:名称[jdbc / znalezisko]未绑定在此Context中。无法找到[jdbc]。       在org.apache.naming.NamingContext.lookup(NamingContext.java:817)       在org.apache.naming.NamingContext.lookup(NamingContext.java:173)       at pl.znalezisko.util.ConnectionProvider.getDataSource(ConnectionProvider.java:25)       在pl.znalezisko.dao.UserDAOImpl。(UserDAOImpl.java:21)       在pl.znalezisko.dao.MysqlDAOFactory.getUserDAO(MysqlDAOFactory.java:12)       在pl.znalezisko.service.UserService.addUser(UserService.java:15)       在pl.znalezisko.controller.RegisterController.doPost(RegisterController.java:29)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:661)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:742)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)       at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)       在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)       在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)       at org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:790)       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1459)       在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)       at java.base / java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)       at java.base / java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)       at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)       在java.base / java.lang.Thread.run(未知来源)   mar 10,20188 12:15:47 PM org.apache.catalina.core.StandardWrapperValve调用   严重:servlet [pl.znalezisko.controller.RegisterController]的Servlet.service()在路径[/ Znalezisko]的上下文中引发了异常   java.lang.IllegalArgumentException:DataSource不能为null       在org.springframework.util.Assert.notNull(Assert.java:112)       在org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate。(NamedParameterJdbcTemplate.java:91)       在pl.znalezisko.dao.UserDAOImpl。(UserDAOImpl.java:21)       在pl.znalezisko.dao.MysqlDAOFactory.getUserDAO(MysqlDAOFactory.java:12)       在pl.znalezisko.service.UserService.addUser(UserService.java:15)       在pl.znalezisko.controller.RegisterController.doPost(RegisterController.java:29)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:661)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:742)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)       at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)       在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)       在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)       at org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:790)       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1459)       在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)       at java.base / java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)       at java.base / java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)       at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)       在java.base / java.lang.Thread.run(未知来源)

1 个答案:

答案 0 :(得分:0)

它可能与拼写错误相关:contex.xml而不是context.xml

或者当您的配置文件不在类路径(context.xml)中时,可能会发生这种情况。