@@ -88,26 +88,8 @@ void test_with_applicationNameAndSchema() throws SQLException {
8888 DataSourcePool pool = DataSourceFactory .create ("app" , ds );
8989 try {
9090 try (Connection connection = pool .getConnection ()) {
91- try (PreparedStatement statement = connection .prepareStatement ("create schema if not exists fred;" )) {
92- statement .execute ();
93- }
94- connection .commit ();
95- try (PreparedStatement statement = connection .prepareStatement ("create table if not exists fred_table (acol integer);" )) {
96- statement .execute ();
97- }
98- try (PreparedStatement statement = connection .prepareStatement ("insert into fred_table (acol) values (?);" )) {
99- statement .setInt (1 , 42 );
100- int rows = statement .executeUpdate ();
101- assertThat (rows ).isEqualTo (1 );
102- }
103- try (PreparedStatement statement = connection .prepareStatement ("select acol from fred.fred_table" )) {
104- try (ResultSet resultSet = statement .executeQuery ()) {
105- while (resultSet .next ()) {
106- int res = resultSet .getInt (1 );
107- assertThat (res ).isEqualTo (42 );
108- }
109- }
110- }
91+ setupTable (connection , "my_table" );
92+ testConnectionWithSelect (connection , "select acol from app.my_table" );
11193 connection .commit ();
11294
11395 try (PreparedStatement statement = connection .prepareStatement ("select application_name from pg_stat_activity where usename = ?" )) {
@@ -124,4 +106,88 @@ void test_with_applicationNameAndSchema() throws SQLException {
124106 pool .shutdown ();
125107 }
126108 }
109+
110+ @ Test
111+ void test_password2 () throws SQLException {
112+ DataSourceConfig ds = new DataSourceConfig ();
113+ ds .setUrl ("jdbc:postgresql://127.0.0.1:9999/app" );
114+ ds .setSchema ("fred" );
115+ ds .setUsername ("db_owner" );
116+ ds .setPassword ("test" );
117+ ds .setPassword2 ("newRolledPassword" );
118+
119+ DataSourcePool pool = DataSourceFactory .create ("app" , ds );
120+ try {
121+ try (Connection connection0 = pool .getConnection ()) {
122+ setupTable (connection0 , "my_table2" );
123+ testConnectionWithSelect (connection0 , "select acol from app.my_table2" );
124+ connection0 .commit ();
125+ try (Connection connection1 = pool .getConnection ()) {
126+ testConnectionWithSelect (connection1 , "select acol from app.my_table2" );
127+ // change password
128+ try (PreparedStatement statement = connection0 .prepareStatement ("alter role db_owner with password 'newRolledPassword'" )) {
129+ statement .execute ();
130+ connection0 .commit ();
131+ }
132+ // existing connections still work
133+ testConnectionWithSelect (connection0 , "select acol from app.my_table2" );
134+ testConnectionWithSelect (connection1 , "select acol from app.my_table2" );
135+
136+ // new connection triggers password switch
137+ try (Connection newConnection0 = pool .getConnection ()) {
138+ testConnectionWithSelect (newConnection0 , "select acol from app.my_table2" );
139+ try (Connection newConnection1 = pool .getConnection ()) {
140+ testConnectionWithSelect (newConnection1 , "select acol from app.my_table2" );
141+ }
142+ }
143+ }
144+
145+ // a new pool switches immediately
146+ DataSourcePool pool2 = DataSourceFactory .create ("app2" , ds );
147+ try (var connP2_0 = pool2 .getConnection ()) {
148+ testConnectionWithSelect (connP2_0 , "select acol from app.my_table2" );
149+ try (var connP2_1 = pool2 .getConnection ()) {
150+ testConnectionWithSelect (connP2_1 , "select acol from app.my_table2" );
151+ try (var connP2_2 = pool2 .getConnection ()) {
152+ testConnectionWithSelect (connP2_2 , "select acol from app.my_table2" );
153+ }
154+ }
155+ }
156+
157+ // reset the password back for other tests
158+ try (PreparedStatement statement = connection0 .prepareStatement ("alter role db_owner with password 'test'" )) {
159+ statement .execute ();
160+ connection0 .commit ();
161+ }
162+ }
163+ } finally {
164+ pool .shutdown ();
165+ }
166+ }
167+
168+
169+ private static void setupTable (Connection connection , String tableName ) throws SQLException {
170+ try (PreparedStatement statement = connection .prepareStatement ("create schema if not exists app;" )) {
171+ statement .execute ();
172+ }
173+ try (PreparedStatement statement = connection .prepareStatement ("create table if not exists app." + tableName + " (acol integer);" )) {
174+ statement .execute ();
175+ }
176+ try (PreparedStatement statement = connection .prepareStatement ("insert into app." + tableName + " (acol) values (?);" )) {
177+ statement .setInt (1 , 42 );
178+ int rows = statement .executeUpdate ();
179+ assertThat (rows ).isEqualTo (1 );
180+ }
181+ }
182+
183+ private static void testConnectionWithSelect (Connection connection , String sql ) throws SQLException {
184+ try (PreparedStatement statement = connection .prepareStatement (sql )) {
185+ try (ResultSet resultSet = statement .executeQuery ()) {
186+ while (resultSet .next ()) {
187+ int res = resultSet .getInt (1 );
188+ assertThat (res ).isEqualTo (42 );
189+ }
190+ }
191+ }
192+ }
127193}
0 commit comments