java - Postgres with Glassfish connection pool -
java - Postgres with Glassfish connection pool -
i new java ee world , want utilize postgresql database in web application. utilize glassfish application server , added connection pool via administration interface (i used site help). don't know "right" way utilize connection pool in application (in fact don't know how connection pool , write simple query).
we need write pretty complex queries, don't know if should create mapping every table , utilize mappings, or utilize sql , kind of row mapper pars results (we used spring rowmapper before).
so question are:
what different ways there utilize connections pool? what (dis)advantages of these patterns? how create clever mapping can handle complicated , performance intensive query's. does work hibernate? , if so, how?
1 - if connection pool configured using glassfish, may develop simple ejb , inject connection using annotation, think best way handle connexion pool.
(all examples compatible hibernate , work on postgresql database)
eg:
@stateless public class myejb { // inject entitymanager @persistencecontext(unitname = "mypu") private entitymanager em; public auto findcarbyid(long carid) { auto acar = (car) em.find(car.class, carid); homecoming acar; } }
the unitname "mypu" jndi name of jdbc resource configured in glassfish administration console.
2 - advantage of pattern is not code dependant, may not alter jdbc ressource regarding dev, test or production environment. jdbc url, user , pass configured on server , not in xml file may alter each time switch environment another. way don't have handle transaction, server commit or rollback on exception. if need handle transaction yourself, may add together annotation under stateless one:
@transactionmanagement(value=transactionmanagementtype.bean)
you need handle transaction like:
em.gettransaction().begin(); seek { // entitymanager em.gettransaction().commit(); } catch(myexception ex) { em.gettransaction().rollback(); }
3 - if need create complexe request, may declare named queries on entity. below possible implementation of auto entity:
@entity public class auto { @namedqueries( { @namedquery(name = "car.findbybrandandcolor", query = "select c auto c.brand = :brand , color = :color") }) }
below illustration of function add together ejb utilize previous named query:
public list<car> findallcarbybrandandcolor(string abrand, string acolor) { list<car> thecars; query thequery = em.createnamedquery("car.findbybrandandcolor"); thequery.setparameter("brand", abrand); thequery.setparameter("color", acolor); seek { thecars = (list<car>) query.getresultlist(); } catch(noresultexception ex) { thecars = null; } homecoming thecars; }
of course, can write named queries complex want (named queries cached , may improve performance), if need query straight database, may utilize native queries below:
// query native sql query thequery = em.createnativequery("select * auto color = 'blue' , brand = 'toyota'", car.class); auto acar = (car) query.getsingleresult();
java postgresql java-ee glassfish connection-pooling
Comments
Post a Comment