Skip to content

Commit

Permalink
4.x: Installs a convenient default for connectionPoolName in UCPBacke…
Browse files Browse the repository at this point in the history
…dDataSourceExtension when appropriate (helidon-io#8359)

* Installs a convenient default for connectionPoolName in UCPBackedDataSourceExtension when appropriate

Signed-off-by: Laird Nelson <laird.nelson@oracle.com>

* Squashable commit; diagnosing test failure that occurs only in the pipeline

Signed-off-by: Laird Nelson <laird.nelson@oracle.com>

* Squashable commit; destroys static connection pools after every test

Signed-off-by: Laird Nelson <laird.nelson@oracle.com>

---------

Signed-off-by: Laird Nelson <laird.nelson@oracle.com>
  • Loading branch information
ljnelson authored and hrstoyanov committed Feb 23, 2024
1 parent 2ff472d commit bfe38b1
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
* Copyright (c) 2019, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -234,6 +234,9 @@ private static PoolDataSource createDataSource(Instance<Object> instance,
}
}
}
if (returnValue.getConnectionPoolName() == null) {
returnValue.setConnectionPoolName(dataSourceName.value());
}
Instance<SSLContext> sslContextInstance = instance.select(SSLContext.class, dataSourceName);
if (!sslContextInstance.isUnsatisfied()) {
returnValue.setSSLContext(sslContextInstance.get());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
* Copyright (c) 2019, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -84,6 +84,7 @@ private void onStartup(@Observes @Initialized(ApplicationScoped.class) final Obj
final PoolDataSourceImpl contextualInstance =
(PoolDataSourceImpl) ((WeldClientProxy) this.test).getMetadata().getContextualInstance();
assertThat(contextualInstance.getDescription(), is("A test datasource"));
assertThat(contextualInstance.getConnectionPoolName(), is("test"));
Connection connection = null;
try {
connection = this.test.getConnection();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.integrations.datasource.ucp.cdi;

import java.sql.SQLException;
import java.util.Properties;

import oracle.ucp.UniversalConnectionPool;
import oracle.ucp.UniversalConnectionPoolAdapter;
import oracle.ucp.UniversalConnectionPoolException;
import oracle.ucp.admin.UniversalConnectionPoolManager;
import oracle.ucp.admin.UniversalConnectionPoolManagerImpl;
import oracle.ucp.jdbc.PoolDataSource;
import oracle.ucp.jdbc.PoolDataSourceFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import static oracle.ucp.jdbc.PoolDataSourceFactory.getPoolDataSource;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.jupiter.api.Assertions.assertThrows;

class TestUcpApi {

TestUcpApi() {
super();
}

@AfterEach
void destroyPools() throws SQLException, UniversalConnectionPoolException {
UniversalConnectionPoolManager ucpManager = UniversalConnectionPoolManagerImpl.getUniversalConnectionPoolManager();
for (String n : ucpManager.getConnectionPoolNames()) {
ucpManager.destroyConnectionPool(n);
}
}

@Test
void testGetPoolDataSourceWithName() throws SQLException {
// Some magic undocumented XML configuration file has not been set as a system property (!), so this call fails
// in an undocumented way.
assertThrows(SQLException.class, () -> getPoolDataSource("bogus"));
}

@Test
void testGetPoolDataSourceWithMinimalProperties() throws SQLException {
Properties p = new Properties();
p.setProperty("connectionPoolName", "bogusConnectionPoolName");
p.setProperty("dataSourceName", "bogusDataSourceName");
// This call fails without some XML file present somewhere (!).
assertThrows(SQLException.class, () -> getPoolDataSource(p));
}

@Test
void testDefaultConnectionPoolNameIsNull() throws SQLException, UniversalConnectionPoolException {
assertThat(getPoolDataSource().getConnectionPoolName(), is(nullValue()));
}

@Test
void testCreateConnectionPoolFailsWithoutSufficientInformation() throws SQLException, UniversalConnectionPoolException {
UniversalConnectionPoolManager ucpManager = UniversalConnectionPoolManagerImpl.getUniversalConnectionPoolManager();
assertThrows(UniversalConnectionPoolException.class,
() -> ucpManager.createConnectionPool((UniversalConnectionPoolAdapter) getPoolDataSource()));
}

@Test
void testCreateConnectionPoolManuallyWithoutName() throws SQLException, UniversalConnectionPoolException {
PoolDataSource pds = getPoolDataSource();
pds.setConnectionFactoryClassName("org.h2.jdbcx.JdbcDataSource");
pds.setURL("jdbc:h2:mem:test");
assertThat(pds.getConnectionPoolName(), is(nullValue()));
assertThat(pds.getDataSourceName(), is(nullValue()));
UniversalConnectionPoolManager ucpManager = UniversalConnectionPoolManagerImpl.getUniversalConnectionPoolManager();
// You can create the pool without a name...
ucpManager.createConnectionPool((UniversalConnectionPoolAdapter)pds);
String[] names = ucpManager.getConnectionPoolNames();
assertThat("Connection pool names: " + java.util.Arrays.asList(names), names.length, is(1));
assertThat(names[0], is(not(nullValue())));
// ...and the name will be auto-generated.
UniversalConnectionPool ucp = ucpManager.getConnectionPool(names[0]);
assertThat(ucp.getName(), is(names[0]));
// This whole API is perhaps surprising: for example, here the creation of the pool modifies the pds that was
// serving as its creation template (!).
assertThat(pds.getConnectionPoolName(), is(names[0]));
// The dataSourceName appears never to be used or modified and may, perhaps, make sense only when the
// aforementioned XML file exists.
assertThat(pds.getDataSourceName(), is(nullValue()));
}

}

0 comments on commit bfe38b1

Please sign in to comment.