Skip to content

Commit

Permalink
Issue eclipse-ee4j#24900 Improve some database test code and README. Add
Browse files Browse the repository at this point in the history
documentation and use try with resources to shorten the code.
  • Loading branch information
escay committed Feb 2, 2025
1 parent 0a59691 commit ce390da
Show file tree
Hide file tree
Showing 8 changed files with 427 additions and 418 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Scenario I:
Scenario 1:
tx {
Bean1 -> getConnection -> insert data into table -> call bean2 -> close connection
Bean2 -> getConnection -> update above data -> close connection
Expand All @@ -9,7 +9,7 @@ tx {
}
---

Scenario II:
Scenario 2:
tx {
Bean1 -> getConnection -> insert data into table -> close connection -> call bean2
Bean2 -> getConnection -> update above data --> close connection
Expand All @@ -20,7 +20,7 @@ tx {
}
---

Scenario III:
Scenario 3:
tx {
Bean1 -> getConnection:c1 -> insert data into table -> getConnection:c2
-> insert some more data -> call bean2 -> close c1, c2
Expand All @@ -32,7 +32,7 @@ tx {
}
---

Scenario IV:
Scenario 4:
tx {
Bean1 -> getConnection:c1 -> insert data into table -> getConnection:c2
-> insert some more data -> close c1,c2 -> call bean2
Expand All @@ -43,11 +43,55 @@ tx {
Bean1 -> new transaction query that data indeed got updated
}

---

Scenario 5:

This test opens 4 connections, and closes the connections in another
order than the connections are opened (which you would not do anymore
when using try with resources).

It is expected that all 4 connections are using the same 1 physical
connection to the database. This test depends on the equals method of
the Connection interface implementation.

---

Scenario 6:

This test is about using 2 datasources and about the connection pool size.
A 100 bean calls are made in the same transaction, default connection pool
size is 32.

Expecting that the implementation reuses connections,
while code runs in the same transaction.

Expecting that getting a resource from a second datasource is possible,
while code runs in the same transaction.

The pool size in datasource: "jdbc-connsharing-pool" is not
initialized with a certain max pool size. Thus default "max-pool-size" in
public class ConnectionPool is set to 32 connections.

Calling the second bean, while first conn1 connection is still open
should be possible, and should not lead to pool problems.
The connection pool maximum should not be reached in this test.

---

Scenario 7:

Adding a test-case (test7 in EJB) for GlassFish-Issue : 15443
Two resource-refs (in this case @Resource injections) for same resource
and doing a physical lookup of same resource (initialContext.lookup), which was failing.
Fix for 15443 should make this test pass.

---

Scenario 8:

Adding a test-case (test8 in EJB) for GlassFish-Issue : 15577 or 15586
Two resources from two different pools (each one with different database, assoc-with-thread=true)
to make sure that connections from approrpiate pool is retrieved from the thread-local.
to make sure that connections from appropriate pool is retrieved from the thread-local.

---
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022, 2025 Contributors to the Eclipse Foundation.
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,14 +17,12 @@

package com.sun.s1asdev.jdbc.connsharing.nonxa.client;

import javax.naming.*;
import java.rmi.*;
import java.util.*;

import com.sun.s1asdev.jdbc.connsharing.nonxa.ejb.SimpleSessionHome;
import com.sun.s1asdev.jdbc.connsharing.nonxa.ejb.SimpleSession;
import com.sun.ejte.ccl.reporter.SimpleReporterAdapter;

import javax.naming.InitialContext;

public class Client {

public static void main(String[] args)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022, 2025 Contributors to the Eclipse Foundation.
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,27 +17,19 @@

package com.sun.s1asdev.jdbc.connsharing.nonxa.ejb;

import jakarta.ejb.*;
import java.rmi.*;
import jakarta.ejb.EJBObject;
import java.rmi.RemoteException;

public interface SimpleSession extends EJBObject {
public boolean test1() throws RemoteException;

public boolean test2() throws RemoteException;

public boolean test3() throws RemoteException;

public boolean test4() throws RemoteException;

public boolean test5() throws RemoteException;

public boolean query() throws RemoteException;

public boolean query2() throws RemoteException;

public boolean test6() throws RemoteException;

public boolean test7() throws RemoteException;

public boolean test8() throws RemoteException;

public boolean query() throws RemoteException;
public boolean query2() throws RemoteException;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022, 2025 Contributors to the Eclipse Foundation.
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,11 +17,11 @@

package com.sun.s1asdev.jdbc.connsharing.nonxa.ejb;

import jakarta.ejb.*;
import java.rmi.*;
import jakarta.ejb.EJBObject;
import java.rmi.RemoteException;

public interface SimpleSession2 extends EJBObject {
public boolean test1() throws RemoteException;
public boolean test2() throws RemoteException;
public boolean test3() throws RemoteException;
public boolean test1UpdateWhereId100() throws RemoteException;
public boolean test2UpdateWhereId200() throws RemoteException;
public boolean test3GetConnection() throws RemoteException;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022, 2025 Contributors to the Eclipse Foundation.
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,21 +17,21 @@

package com.sun.s1asdev.jdbc.connsharing.nonxa.ejb;

import jakarta.ejb.*;
import javax.naming.*;
import javax.sql.*;
import java.rmi.*;
import java.util.*;
import java.sql.*;
import jakarta.transaction.UserTransaction;
import jakarta.ejb.CreateException;
import jakarta.ejb.SessionBean;
import jakarta.ejb.SessionContext;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class SimpleSession2Bean implements SessionBean
{
public class SimpleSession2Bean implements SessionBean {

private SessionContext ctxt_;
private InitialContext ic_;

public void setSessionContext(SessionContext context) {
ctxt_ = context;
try {
ic_ = new InitialContext();
} catch( NamingException ne ) {
Expand All @@ -41,83 +42,75 @@ public void setSessionContext(SessionContext context) {
public void ejbCreate() throws CreateException {
}

/**
*/
public boolean test1() throws Exception {
DataSource ds = (DataSource)ic_.lookup("java:comp/env/DataSource2");
Connection conn1 = null;
Statement stmt1 = null;
ResultSet rs1 = null;
boolean passed = false;
public boolean test1UpdateWhereId100() throws Exception {
logStartTest("test1UpdateWhereId100");
DataSource ds = (DataSource) ic_.lookup("java:comp/env/DataSource2");

try {
conn1 = ds.getConnection();
stmt1 = conn1.createStatement();
stmt1.executeUpdate( "UPDATE CONNSHARING SET c_phone='CONN_SHARING_BEAN_2' WHERE c_id=100");
try (Connection conn1 = ds.getConnection();
Statement stmt1 = conn1.createStatement()) {
getPhysicalConnectionAndLog(ds, conn1);
stmt1.executeUpdate("UPDATE CONNSHARING SET c_phone='CONN_SHARING_BEAN_2' WHERE c_id=100");

return true;
} catch( SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
if (stmt1 != null) {
try { stmt1.close(); } catch( Exception e1 ) {}
}
if (conn1 != null) {
try { conn1.close(); } catch( Exception e1 ) {}
}
}

}

public boolean test2() throws Exception {
DataSource ds = (DataSource)ic_.lookup("java:comp/env/DataSource2");
Connection conn1 = null;
Statement stmt1 = null;
ResultSet rs1 = null;
boolean passed = false;
public boolean test2UpdateWhereId200() throws Exception {
logStartTest("test2UpdateWhereId200");
DataSource ds = (DataSource) ic_.lookup("java:comp/env/DataSource2");

try {
conn1 = ds.getConnection();
stmt1 = conn1.createStatement();
try (Connection conn1 = ds.getConnection();
Statement stmt1 = conn1.createStatement()) {
getPhysicalConnectionAndLog(ds, conn1);
stmt1.executeUpdate( "UPDATE CONNSHARING SET c_phone='CONN_SHARING_BEAN_2_2' WHERE c_id=200");

return true;
} catch( SQLException e) {
e.printStackTrace();
return false;
} finally {
if (stmt1 != null) {
try { stmt1.close(); } catch( Exception e1 ) {}
}
if (conn1 != null) {
try { conn1.close(); } catch( Exception e1 ) {}
}
}

}

public boolean test3() throws Exception {
public boolean test3GetConnection() throws Exception {
logStartTest("test3GetConnection");
DataSource ds = (DataSource) ic_.lookup("java:comp/env/DataSource2");
Connection conn1 = null;
boolean passed = false;

try {
conn1 = ds.getConnection();
try (Connection conn1 = ds.getConnection()) {
getPhysicalConnectionAndLog(ds, conn1);
passed = true;
} catch (SQLException e) {
e.printStackTrace();
passed = false;
} finally {
try {
if(conn1 != null)
conn1.close();
} catch (Exception e1) {
}
}

return passed;
}

/**
* Returns the physical connection and logs it. This is useful to see what
* happens if the server implementation is changed and these tests start to
* fail: it allows comparison of connection logic.
*/
private static Connection getPhysicalConnectionAndLog(DataSource ds,
Connection connection) throws SQLException {
if (ds instanceof com.sun.appserv.jdbc.DataSource) {
Connection physicalConnection = ((com.sun.appserv.jdbc.DataSource) ds).getConnection(connection);

// Log both physical connection and the application server Connection
System.out.println("Physical connection: " + physicalConnection
+ " returned for Connection: " + connection);
return physicalConnection;
}
throw new IllegalStateException("Expecting com.sun.appserv.jdbc.DataSource");
}

private void logStartTest(String testName) {
System.out.println(this.getClass().getName() + " - Start " + testName);
}

public void ejbLoad() {}
public void ejbStore() {}
public void ejbRemove() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022, 2025 Contributors to the Eclipse Foundation.
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,8 +17,9 @@

package com.sun.s1asdev.jdbc.connsharing.nonxa.ejb;

import jakarta.ejb.*;
import java.rmi.*;
import jakarta.ejb.CreateException;
import jakarta.ejb.EJBHome;
import java.rmi.RemoteException;

public interface SimpleSession2Home extends EJBHome
{
Expand Down
Loading

0 comments on commit ce390da

Please sign in to comment.