1

How I can set a Data Source dynamically? Like below in Spring:

    @Bean(name = "dataSource")
    public DriverManagerDataSource dataSource() {
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/userbase");
        driverManagerDataSource.setUsername("root");
        driverManagerDataSource.setPassword("root");
        return driverManagerDataSource;
    }

Can I import just this functionality from Spring to a Java EE 7 enterprise application?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    You should switch over to using a dataSource instead of a dataSource manager. – Matthew Fontana Jun 01 '16 at 15:28
  • 1
    this post might help you : http://stackoverflow.com/questions/13507522/dynamically-change-spring-data-source – Mohamed Nabli Jun 01 '16 at 16:06
  • @MatthewFontana are you referring to http://docs.oracle.com/javaee/7/api/javax/annotation/sql/DataSourceDefinition.html ? – Gabriel Patricio Bonilla Jun 01 '16 at 19:23
  • 1
    @PatricioBonilla sorry for the confusion I am just stating for a Java EE application you would prefer a straight datasource instead of a driver manager. You can find a decent discussions on the differences here: http://stackoverflow.com/questions/15198319/why-do-we-use-a-datasource-instead-of-a-drivermanager As for which datasource to use I typically recommend a spring variant. Spring simplifies a Java EE developer's life immensely – Matthew Fontana Jun 02 '16 at 13:07

1 Answers1

1

Any Java EE 7 implementation will have facilities for defining a javax.sql.DataSource that has a name in the server's JNDI directory.

You can inject a reference to it in your application client class using the javax.annotations.Resource annotation and the use it something like:

public class SomeDatabaseClient {

    @Resource("jdbc/myDataSource")
    javax.sql.DataSource myDataSource;

    public void useTheDatabase(String username, String password) {

         try (Connection con = myDataSource.getConnection(username, password);
              PreparedStatement ps = con.prepareStatement(...);
              ResultSet rs = ps.executeQuery() {
              // process the result set
             ...
         } catch (SQLException e) {
              // handle errors
         }
    }
}

If you don't need to specify the database credentials dynamically they can be included in the DataSource definition in the server.

Note that managing user passwords securely is not trivial so you need a pretty good reason to be using them in this fashion.

You can also set up your Spring Framework configuration to work like this with the same code if that is what you need.

Steve C
  • 18,876
  • 5
  • 34
  • 37
  • I get the impression that OP explicitly want to create and manage the data source in webapp end instead of in server end (which is indeed strange and fishy, but it is what his Spring snippet actually does). – BalusC Jun 03 '16 at 13:50
  • @Steve C, I actually don't want to use frameworks, just work with Java EE 7. It's a requirement of my enterprise. The reason is for manage audit information in DB's tables. You know trigger :NEW.[FieldName] := USER; (Oracle) The user logs in with their credentials. I saw this Spring snippet and looks like the functionality that I actually want. Or do you know any optimal approach to get it? – Gabriel Patricio Bonilla Jun 03 '16 at 19:18
  • 1
    The solution above is pure Java EE - no frameworks required – Steve C Jun 04 '16 at 00:24