在Tomcat war app上加载SQLite文件时出错

时间:2016-09-13 06:56:36

标签: java sqlite tomcat

我正在尝试在我的tomcat应用程序上使用并嵌入sqlite.db文件。我正在研究NetBeans并通过Netbean的播放按钮在本地服务器上部署它。到现在为止还挺好。但是,当我构建war文件并将其部署在Raspberry PI tomcat上时,我遇到了问题。我附上以下错误。

由于som原因,在本地工作的相同代码不能在Raspberry上安装的这个tomcat服务器上运行。有什么想法吗?

谢谢!

====代码====

ContextService.java

public class ContexService implements ServletContextListener {


    /**
     * 
     * @param sce 
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {

        System.out.println("== context initialized ==");
        SqliteTest.getInstance().start();
        SqliteTest.getInstance().stop();

    }

    /**
     * 
     * @param sce 
     */
    @Override
    public void contextDestroyed(ServletContextEvent sce) {

        System.out.println("== context destroyed ==");
        SqliteTest.getInstance().stop();
    }

}

SQLiteManager.java

public class SQLiteManager {

    private static final Logger logger = LoggerFactory.getLogger(SQLiteManager.class);
    private static SQLiteManager instance = null;
    private static Connection connection = null;

    private static int QUERY_TIMEOUT = 5;
    private static String TABLE_PROPERTIES = "Properties";
    private static String DB_NAME = "safemo.db";

    private static class TABLE_PROPERTIES_COLUMNS {

        public static final String TRIP_NUMBER = "tripNumber";
    }

    private SQLiteManager() {
        openDB();
    }

    public static SQLiteManager getInstance() {
        if (instance == null) {
            instance = new SQLiteManager();
        }

        return instance;

    }

    private void openDB() {
        if (connection == null) {
               connect();
        } else {
            try {
                if (connection.isClosed()) {
                    connect();
                }
            } catch (SQLException ex) {
                logger.error("Error openning connection : " + ex.getMessage());
            }
        }
    }

    private void connect() {

        try {
            Class.forName("org.sqlite.JDBC");
            connection = DriverManager.getConnection("jdbc:sqlite::resource:" + DB_NAME);

            logger.debug("Opened database successfully");
        } catch (ClassNotFoundException ex) {
            logger.error(ex.getMessage());
        } catch (SQLException ex) {
            logger.error(ex.getMessage());
        }

    }

    public void closeDB() {
        try {
            if (connection != null) {
                connection.close();
            }
            logger.debug("Connection closed successfully");
        } catch (SQLException ex) {
            logger.error("Error closing database : " + ex.getMessage());
        }
    }

    public void incrementTripNumber()
    {
         try {
            Statement statement = connection.createStatement();
            statement.setQueryTimeout(QUERY_TIMEOUT);  // set timeout to 30 sec.
            statement.executeUpdate("UPDATE " + TABLE_PROPERTIES + " SET " + TABLE_PROPERTIES_COLUMNS.TRIP_NUMBER + " = " + TABLE_PROPERTIES_COLUMNS.TRIP_NUMBER + " + 1");
        } catch (SQLException ex) {
            logger.error("Error getting trip number : " + ex.getMessage());
        }

    }

    public int getTripNumber() {

        int tripNumber = -1;

        try {
            Statement statement = connection.createStatement();
            statement.setQueryTimeout(QUERY_TIMEOUT);  // set timeout to 30 sec.
            ResultSet rs = statement.executeQuery("SELECT " + TABLE_PROPERTIES_COLUMNS.TRIP_NUMBER + " FROM " + TABLE_PROPERTIES);

            if (rs.next()) {
                tripNumber = rs.getInt(TABLE_PROPERTIES_COLUMNS.TRIP_NUMBER);
            }

            rs.close();

        } catch (SQLException ex) {
            logger.error("Error getting trip number : " + ex.getMessage());
        } finally {
            return tripNumber;
        }
    }
}

SQLiteTest.java

public class SqliteTest 
{
    private static SqliteTest instance = null;
     private static final Logger logger = LoggerFactory.getLogger(SqliteTest.class);

    private SqliteTest()
    {

    }

    public static SqliteTest getInstance()
    {
        if(instance == null)
        {
            instance = new SqliteTest();
        }

         return instance;

    }

    public void start()
    {
        SQLiteManager sqliteManager = SQLiteManager.getInstance();
        logger.debug("TRIP NUMBER : " + sqliteManager.getTripNumber());
        sqliteManager.incrementTripNumber();
        logger.debug("TRIP NUMBER INCREASED: " + sqliteManager.getTripNumber());

    }

    public void stop()
    {
        SQLiteManager.getInstance().closeDB();
    }
}

Pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.app</groupId>
    <artifactId>SqliteTest</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <name>SqliteTest</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

         <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.21</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.7</version>
        </dependency>

        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>

          <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.8.11.2</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Netbeans本地安装上的控制台输出

== context initialized ==
08:40:55.398 [http-nio-8080-exec-18] DEBUG SQLiteManager - Opened database successfully
08:40:55.406 [http-nio-8080-exec-18] DEBUG SqliteTest - TRIP NUMBER : 2
08:40:55.543 [http-nio-8080-exec-18] DEBUG SqliteTest - TRIP NUMBER INCREASED: 3
08:40:55.543 [http-nio-8080-exec-18] DEBUG SQLiteManager - Connection closed successfully 

这是我在构建war文件并通过Tomcat管理器在Raspberry上部署时出现的错误。

== context initialized ==
Aug 25, 2016 6:05:33 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
Aug 25, 2016 6:05:33 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/SqliteTest-1.0] startup failed due to previous errors
== context destroyed ==
Aug 25, 2016 6:05:33 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesJdbc
WARNING: The web application [/SqliteTest-1.0] registered the JDBC driver [org.sqlite.JDBC] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
Aug 25, 2016 6:05:33 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deployment of web application archive /var/lib/tomcat8/webapps/SqliteTest-1.0.war has finished in 42,719 ms

Raspberry上的java版本: openjdk版“1.8.0_40-internal” OpenJDK运行时环境(build 1.8.0_40-internal-b04) OpenJDK Zero VM(版本25.40-b08,解释模式)

Tomcat版本:Apache Tomcat / 8.0.14(Debian) Tomcat JVM 1.8.0_40-internal-b04

0 个答案:

没有答案