Fujitsu Logo
ENQUIRE

    Knowledge articles - Application development

    icon-kb-article-appdev-color-simple

    How do I connect to a database from my application using JDBC driver?KB7001

    You can connect to a database via an application using the DriverManager, PGConnectionPoolDataSource, and PGXADataSource JDBC driver classes.

    The following is an example of using the PGConnectionPoolDataSource class to connect to a database:

    import java.sql. *;
    import org.postgresql.ds.PGConnectionPoolDataSource;
    ...
    PGConnectionPoolDataSource source = new PGConnectionPoolDataSource ();
    source.setServerName ("sv1");
    source.setPortNumber (27500);
    source.setDatabaseName ("mydb");
    source.setUser ("myuser");
    source.setPassword ("myuser01");
    source.setLoginTimeout (20);
    source.setSocketTimeout (20);
    ...
    Connection con = source.getConnection ();

    For details, refer to PostgreSQL 13 SP1 Documentation > Chapter 2 - JDBC Driver > 2.3- Connecting to the Database.

    Applicable to

    Product: FUJITSU Enterprise Postgres SE, FUJITSU Enterprise Postgres AE, PostgreSQL

    Architecture: X86, S390x

    Operating System: Windows, Linux

    Versions: from 9.5

    How do I cancel a query that has been running for a long time, or break a connection that has been waiting for a long time?KB7002

    The query can be cancelled by using the pg_cancel_backend function, and connections can be forcibly broken using either the pg_terminate_backend function or pgAdmin.

    In the pg_cancel_backend and pg_terminate_backend functions, the process ID specifies the query to cancel or connection to break. The process ID can be found in the pg_stat_activity view or with the OS ps command.

    Applicable to

    Product: FUJITSU Enterprise Postgres SE, FUJITSU Enterprise Postgres AE, PostgreSQL

    Architecture: X86, S390x

    Operating System: Windows, Linux

    Versions: from 9.5

    An application using JDBC driver shows the error message The new connection was automatically closed because the same PooledConnection was opened or PooledConnection is already closed.KB7003

    This may have happened because the application was trying to work on a Connection object that had already been closed.

    Review your application's handling of Connection objects as follows:

    • Are you executing a method on a Connection object that has already been closed?
    • Did you execute the getConnection method multiple times on the same instance of the PGPooledConnection or PGXAConnection class, and not on the Connection object returned by the last execution?
    • When you execute the getConnection method on a PGPooledConnection or PGXAConnection class, the relationship between these classes and the getConnection method to be executed is one-to-one. If the getConnection method is executed more than once using an instance of the same class, the previously created Connection object is closed.

    Applicable to

    Product: FUJITSU Enterprise Postgres SE, FUJITSU Enterprise Postgres AE, PostgreSQL

    Architecture: X86, S390x

    Operating System: Windows, Linux

    Versions: from 9.5

    A System.OutOfMemoryException exception was raised when running a .NET application.KB7004

    Possible causes include the following:

    • ExecuteReader() is running on a table with binary column data types or other large data structures
    • The connection string to the database specifies PreloadReader = true
    The above execution or specification holds the data for the entire result set in memory. This may have consumed a large amount of memory and run out of memory.

    Resolution

    Consider making the following changes:

    • Change ExecuteReader() to ExecuteReader(CommandBehavior.SequentialAccess)
    • Change PreloadReader = True to PreloadReader = False
    For more information, refer to:

    Applicable to

    Product: FUJITSU Enterprise Postgres SE, FUJITSU Enterprise Postgres AE, PostgreSQL

    Architecture: X86, S390x

    Operating System: Windows, Linux

    Versions: from 9.5

    Read our latest blogs

    Read our most recent articles regarding all aspects of PostgreSQL and FUJITSU Enterprise Postgres.