警告:RAR8054:为池[AffablebeanPool]创建非池化[test]连接时出现异常,null

时间:2011-08-12 08:26:18

标签: java jdbc glassfish

使用mysql驱动程序和glassfish服务器连接到连接池和jdbc资源时出现上述错误。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

/**
 *
 * @author LenasalonM01
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.swing.JOptionPane;

public class database {

    private Connection connect;
    private Statement statement;
    private ResultSet resultset;
    private InitialContext context;
    private DataSource datasource;

    public void connection() {
        try {

            Class.forName("com.mysql.jdbc.Driver").newInstance();
            connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/hrms", "root","");
             context = new InitialContext();
            datasource = (DataSource)context.lookup("jdbc/hrms");
            connect = datasource.getConnection();

            statement = connect.createStatement();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }

    }

    public void disconnect() {
        try {
            if (resultset != null) {
                resultset.close();
            }
            if (connect != null) {
                connect.close();
            }
            if (context != null) {
                context.close();
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }

    public boolean checkValid(String query) {
        try {
            connection();
            resultset = statement.executeQuery(query);
            if (resultset.next()) {
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
            return false;
        }
    }

    public ResultSet fetchdata(String query) {
        try {
            connection();
            resultset = statement.executeQuery(query);
            disconnect();
            return resultset;

        } catch (Exception e) {
            return resultset;

        }
//        return resultset;
    }
//    public ResultSet insertdata(){
//
//    }
}

服务器资源代码如下

<resources>
  <jdbc-resource enabled="true" jndi-name="jdbc/hrms" object-type="user" pool-name="AffablebeanPool">
    <description/>
  </jdbc-resource>
  <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="AffablebeanPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.ConnectionPoolDataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
    <property name="URL" value="jdbc:mysql://localhost:3306/hrms"/>
    <property name="User" value="root"/>
    <property name="Password" value=""/>
  </jdbc-connection-pool>
</resources>

1 个答案:

答案 0 :(得分:0)

我在解决了link

中的其他类似问题之后得到了解决方法