0

I want to work with 2+ beans with database access, but I'm having problem accessing same database.

My first connection works fine, I'm getting the results I want.

    @DataSourceDefinition(
         name = "java:global/***/***",
         className = "org.apache.derby.jdbc.ClientDataSource",
         url = "jdbc:derby://localhost:1527/***",
         databaseName = "***",
         user = "***",
         password = "***"
    )
    @Named(value = "userRepo")
    @SessionScoped
    public class UserRepo implements Serializable {
        @Resource(lookup = "java:global/***/***")
        DataSource dataSource;

        public boolean validate() throws SQLException {
            ResultSet rs = null;
            if (dataSource == null) {
                throw new SQLException("Unable to obtain DataSource");
            }
            Connection connection = dataSource.getConnection();
            if (connection == null) {
                throw new SQLException("Unable to connect to DataSource");
            }
            try {
                PreparedStatement ps = connection.prepareStatement("select 
                    email, password from customer " + "where email=? and 
                        password=?");
                ps.setString(1, getEmail());
                ps.setString(2, getPassword());
                rs = ps.executeQuery();
                while (rs.next()) {
                    tempEmail = rs.getString("email");
                    tempPassword = rs.getString("password");
                }
            } // end try
            finally {
                connection.close();
            }
        }

When I try to use this on another bean with same method, it doesn't return wanted results (even with exact same query)

    @DataSourceDefinition(
         name = "java:global/***/***",
         className = "org.apache.derby.jdbc.ClientDataSource",
         url = "jdbc:derby://localhost:1527/***",
         databaseName = "***",
         user = "***",
         password = "***"
    )

    @Named(value = "customer")
    @SessionScoped
    public class Customer implements Serializable {
        @Resource(lookup = "java:global/***/***")
        DataSource dataSource;
        ...
        ...
    }

I tried: removing connection closing, injecting "UserRepo" bean with @Inject and use its datasource with userRepo.dataSource but no luck.

I guess, I can create all queries in "UserRepo" bean, but that seems awfully bad coding practice and godclasses are hard to handle.

Is there any way I can work on these 2 separate beans? Injection won't be problem.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    Bad design to define your database stuff on jsf managed beans. https://stackoverflow.com/questions/30639785/jsf-controller-service-and-dao – Kukeltje May 13 '18 at 00:48
  • @Kukeltje You are right, but that answer is mostly constructed on spring framework and your answer is not related with question tags checkout this: https://www.mkyong.com/jsf2/jsf-2-0-jdbc-integration-example/ . I am using this+university lecture, i couldn't find good example/explanation where 2 beans using same connection pool. –  May 13 '18 at 01:10
  • 1
    Sorry, nonsense. The link I refer to is a very generic non spring specific way of doing things in general. You should just not do jdbc in jsf controllers. Therefor you cannot find a lot of examples where 2 jsf beans share a connection since connections should not be used in jsf beans but in service classes. The mykong example is over simplified – Kukeltje May 13 '18 at 01:23

0 Answers0