--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/683d0723-1a09-4091-994b-9d0de5b80e70%40googlegroups.com.
try (Connection conn = DB.getConnection()) { DSLContext dslContext = DSL.using(conn, SQLDialect.MYSQL);
// Example 2: Custom Pojos playersList = dslContext.select().from(Players.PLAYERS).fetch().into(Players.class);
return ok(players.render(playersList));}catch (Exception e) { e.printStackTrace();}
// return errorspublic class JooqConnectionProvider implements ConnectionProvider {
Database db;
private Connection connection = null;
public JooqConnectionProvider(Database database){ db = database; }
@Override public java.sql.Connection acquire() throws DataAccessException { try { connection = db.getConnection(); return connection; } catch (DataAccessException ex) { throw new DataAccessException("Error getting connection from data source", ex); } }
@Override public void release(java.sql.Connection releasedConnection) throws DataAccessException { if (connection != releasedConnection) { throw new IllegalArgumentException("Expected " + connection + " but got " + releasedConnection); } try { connection.close(); connection = null; } catch (SQLException e) { throw new DataAccessException("Error closing connection " + connection, e); } }}
public class JooqContextProvider {
@Inject Database db;
public DSLContext dsl() { return DSL.using( new JooqConnectionProvider(db), SQLDialect.MYSQL); }}
// Then in my code, I inject the JooqContextProvider and use it.@InjectJooqContextProvider jooq;
public List<Players> fetchAll(){ return jooq.dsl() .select() .from(Players.PLAYERS) .fetchInto(Players.class);}public DSLContext dsl() { return DSL.using( new JooqConnectionProvider(db), SQLDialect.MYSQL);}
public PlayerDao playerDao(){ Configuration config = new DefaultConfiguration() .set(new JooqConnectionProvider(db)) .set(SQLDialect.MYSQL); return new PlayerDao(config);}
public class JooqContextProvider {
@Inject Database db;
public DSLContext dsl() { return DSL.using(db.getConnection(),SQLDialect.MYSQL); }
public PlayerDao playerDao(){ Configuration config = new DefaultConfiguration() .set(db.getConnection()) .set(SQLDialect.MYSQL);
return new PlayerDao(config); }}
// Then I would call into this class like this...
return jooq.playerDao().fetchOneByPlayerId(id);
return jooq.dsl().select().from(PLAYERS_TABLE).fetchInto(Player.class);To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/3c054975-5f7c-4620-9ac4-6d5abb4bbd7a%40googlegroups.com.--
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/tXvA76ZQsJs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/CADRTJga965et2d36VX%2B%2ByeRUjzrTLUJM_UDy2te%2BJnsjSoqsfA%40mail.gmail.com.
Lukas,Thanks for your suggestions. I have a few questions to clarify, to make sure to understand it.1. Try to avoid state in your connection providerIf I am following you correctly, "best to just remove the connection" and "caching the connection" and "we have that covered." Is there any reason for me to have my JooqConnectionProvider at all then? Should I just remove the class entirely because Jooq is already doing this for me?
2. The JOOQ transaction API and 3. Abstractice your wrapper...I can use an approach like this where I have similar queries, such as the fetch me all of type E, as you have shown. I will try to use that where I can.What I am trying to do is understand JOOQ enough to use it correctly within my app and make it reusable for my project.If I have a controller with 5 methods in it, I will be calling JOOQ (via a service I write) five times. Every example I see for JOOQ has that connectionProvider (or configuration) and SQLDialect passed into the DSL.using(). I believe I need to create that connectionProvider (or configuration, or connection) each time I query with JOOQ, is that correct?
As in, I cannot create a connectionProvider (or configuration) once in my controller's constructor and use it for all five of my method calls? I believe I need to new that up each time. That is where the abstraction and reusability examples will help.Also, for DSL.using(configuration), what is a good example of how to new up that configuration? I have not found a good example of that, and this is what I have below.public DSLContext dsl() {return DSL.using(new JooqConnectionProvider(db),SQLDialect.MYSQL);}public PlayerDao playerDao(){Configuration config = new DefaultConfiguration().set(new JooqConnectionProvider(db)).set(SQLDialect.MYSQL);return new PlayerDao(config);}
If I remove my ConnectionProvider class, I would have this class, as my JOOQ data wrapper.public class JooqContextProvider {@InjectDatabase db;public DSLContext dsl() {return DSL.using(db.getConnection(),SQLDialect.MYSQL);}public PlayerDao playerDao(){Configuration config = new DefaultConfiguration().set(db.getConnection()).set(SQLDialect.MYSQL);return new PlayerDao(config);}}// Then I would call into this class like this...return jooq.playerDao().fetchOneByPlayerId(id);return jooq.dsl().select().from(PLAYERS_TABLE).fetchInto(Player.class);
I do not want to write DSL.using(connectionProvider, SQLDialect.MYSQL) five tims.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/CAB4ELO79OJWYABH9SUHZnUu%2Bs%3DJoPLjkTZFjTVXYfXBrWRWr9w%40mail.gmail.com.
As a java 8 fan who's hoping not to have any Scala in our stack besides a teensy bit for Twirl templates, Slick isn't an option. Also, I believe jOOQ is as far ahead of Anorm as Play 2's Twirl is ahead of Play 1's groovy templates.
> Maybe, we can come up with an example in the jOOQ GitHub repository...I would love that! But I sure understand we're all busy :) jOOQ and Twirl just seem like such a great pairing though, can't wait to see them better supported as a couple.
Hi Edgar,Indeed the jOOQ and Play teams are disjoint. This is probably due to the Play vendor also trying to promote the second-best Scala/SQL API commonly referred to as Slick - so they don't want to help with a competitor product integration :) We would love to contribute more to Lightbend, but thus far, we have not received any positive reply on this matter.
--
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/tXvA76ZQsJs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/CAJmgB63p5vtUnhpx55ZuRaVtGD_RkEt79LUgKvM1eS9q0U7P-A%40mail.gmail.com.