postgresql - Hibernate prints an error on unique constraint violation -


just clear, i'm not seeing exceptions thrown, error statement printed console.

also, understand why it's failing unique constraint, in fact, way i'm going this, should , want catch exception , handle accordingly. basically, if try insert duplicate, want catch exception , select row out corresponding pojo table can refer it.

i have looked @ bunch of other posts / solutions , seems should able catch 1 of few different exceptions or it's been suggested select first, if select first, technically, still have race condition because producer of data (twitter4j) can have multiple threads. so, if try insert first, 1 of threads win , others fall select.

my problem is, upon trying insert duplicate row, not throw exception me catch, prints following error:

sql error: 0, sqlstate: 23505    error: duplicate key value violates unique constraint "user_screen_name_key"   detail: key (screen_name)=(xxxx) exists.   hhh000010: on release of batch still contained jdbc statements 

i put in println's , looks it's occurring on session.save(usermodel) call. looks swallowing exception...

when first started testing this, able catch javax.persistence.persistenceexception , check it's cause see if instanceof constraintviolationexception, that's no longer being thrown reason. i've since added infinispan caching, i'm not sure why behave this.

any suggestions?

relevant snippet of pom.xml

<dependency>     <groupid>org.hibernate</groupid>     <artifactid>hibernate-core</artifactid>     <version>5.2.10.final</version> </dependency> <dependency>     <groupid>org.hibernate</groupid>     <artifactid>hibernate-infinispan</artifactid>     <version>5.2.10.final</version> </dependency> <dependency>     <groupid>org.hibernate</groupid>     <artifactid>hibernate-entitymanager</artifactid>     <version>5.2.10.final</version> </dependency> <dependency>     <groupid>org.hibernate</groupid>     <artifactid>hibernate-spatial</artifactid>     <version>5.2.10.final</version> </dependency> <dependency>     <groupid>org.postgresql</groupid>     <artifactid>postgresql</artifactid>     <version>42.1.4</version> </dependency> <dependency>     <groupid>net.postgis</groupid>     <artifactid>postgis-jdbc</artifactid>     <version>2.2.1</version> </dependency> <dependency>     <groupid>org.infinispan</groupid>     <artifactid>infinispan-embedded</artifactid>     <version>9.1.0.final</version> </dependency> 

snippet pojo:

@entity @table(name = "user", schema = "twitter", uniqueconstraints = {     @uniqueconstraint(columnnames = "screen_name"),     @uniqueconstraint(columnnames = "twitter_id")}) @namedqueries({     @namedquery(         name = "get_user_by_id",         query = "select user com.myapp.rds.models.twitter.user user "                 + "where user.twitterid = :twitterid",         fetchsize = 1,         cachemode = cachemodetype.normal,         cacheable = true,         cacheregion = "twitter_users",         comment = "look twitter user profile twitter id"     ) }) @cache(region = "twitter_users", usage = cacheconcurrencystrategy.read_only) @cacheable(value = true) public class user implements java.io.serializable {      private static final long serialversionuid = 1l;     public static final string get_user_by_twitter_id = "get_user_by_id";     public static final string twitter_id_parameter = "twitterid";      @id     @sequencegenerator(name = "user_seq", schema = "twitter", sequencename = "user_id_seq", allocationsize = 1)     @generatedvalue(strategy = generationtype.auto, generator = "user_seq")     @column(name = "id", unique = true)     private int id;      @manytoone(fetch = fetchtype.lazy)     @joincolumn(name = "profile_lang_id", nullable = false)     private lang lang;      @column(name = "name", nullable = false)     private string name;      @column(name = "screen_name", unique = true, nullable = false)     private string screenname;      @column(name = "description")     private string description;      @column(name = "geo_enabled")     private boolean geoenabled;      @column(name = "twitter_id", unique = true, nullable = false)     private string twitterid;      @column(name = "protected")     private boolean protected_;      @column(name = "time_zone")     private string timezone;      @column(name = "utc_offset")     private integer utcoffset;      @column(name = "profile_location")     private string profilelocation;      @temporal(temporaltype.timestamp)     @column(name = "profile_created_at", nullable = false, length = 29)     private date profilecreatedat;      @temporal(temporaltype.timestamp)     @column(name = "created_at", length = 29, nullable = true, insertable = false)     private date createdat;      @onetomany(fetch = fetchtype.lazy, mappedby = "user")     @lazycollection(value = lazycollectionoption.extra)     private set<status> statuses = new hashset(0);      @onetomany(fetch = fetchtype.lazy, mappedby = "user")     @lazycollection(value = lazycollectionoption.extra)     private set<twitteruserprofileentityinstancemap> twitteruserprofileentityinstancemaps = new hashset(0);      //setters , getters 

snippet hibernate.cfg.xml file:

<property name="hibernate.dialect">org.hibernate.spatial.dialect.postgis.postgisdialect</property> <property name="hibernate.connection.driver_class">org.postgis.driverwrapper</property> <property name="hibernate.connection.url">jdbc:postgresql_postgis://127.0.0.1:5432/db</property> <property name="hibernate.connection.username">username</property> <property name="hibernate.connection.password">password</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.generate_statistics">true</property> <property name="hibernate.use_sql_comments">true</property> <property name="hibernate.hbm2ddl.auto">validate</property> <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.use_query_cache">true</property> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.infinispan.infinispanregionfactory</property> <property name="hibernate.cache.infinispan.cfg">org/hibernate/cache/infinispan/builder/infinispan-configs-local.xml</property> 

the code driving this:

transaction tx = session.begintransaction(); user usermodel = new user(); usermodel.setscreenname(user.getscreenname()); //more setters  try {     integer id = (integer) session.save(usermodel);     system.out.println("saving user");     usermodel.setid(id); } catch (persistenceexception pe) {     system.out.println("loading user");     if (pe.getcause() instanceof constraintviolationexception) {         optional<user> lookup = session                 .getnamedquery(user.get_user_by_twitter_id)                 .setparameter(user.twitter_id_parameter, string.valueof(user.getid()))                 .uniqueresultoptional();          if (lookup.ispresent()) {             return lookup.get();         }          system.out.println("failed load user: " + user.getscreenname() + " (" + user.getid() + ")");     } }  session.flush(); tx.commit(); 

edit 1 tried switching jpa entitymanager perform persisting of entity, behaved similarly. this:

hhh000346 error during managed flush [org.hibernate.exception.constraintviolationexception: not execute statement] 

again, no exception, no stacktrace.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -