when case planshipdate<today sqlsever & mysql - mysql

i need to show all records when
planshipdate<today ==> late collection
plandeliverdate<today ==> late delivery
status name in (tendered, booked) ==> no EDI update

You seem to know it's CASE WHEN:
SELECT
CASE
WHEN planshipdate<NOW() THEN 'late collection'
WHEN plandeliverdate<NOW() THEN 'late delivery'
WHEN statusname in ('tendered', 'booked') THEN 'no EDI update'
END
FROM
...
You just need to decide which database you're using and swap the NOW for getUtcDate if it is sql server

Related

rescue how to raise further or forget an exception

How do I raise an exception further in eiffel? I have 3 cases
I want to retry
a_feature
local
l_retries_count: INTEGER
do
some_potential_failing_feature
rescue
if l_retries_count <= 3 then
l_retries_count := l_retries_count + 1
retry
end
end
I want to do close db connection and ignore exception
a_feature
do
some_potential_failing_feature
rescue
db_connection.close
end
I want to do close db connection and send an email to an admin and ignore
a_feature
do
some_potential_failing_feature
rescue
db_connection.close
send_email_to_admin
end
I want to close db_connection and raise the exception further, I'll put all the cases I can think about into the above code
a_feature
local
l_retries_count: INTEGER
do
some_potential_failing_feature
rescue
if l_retries_count <= 3 then
l_retries_count := l_retries_count + 1
log_error ("Error, retrying for " + l_retries_count.out + "th time")
retry
else
db_connection.close
send_email_to_admin
-- raise the_created_exception_from_some_potential_failing_feature -- how do I do that?
end
end
You can try one of the following:
{EXCEPTION_MANAGER}.last_exception.original.raise
{EXCEPTION_MANAGER}.last_exception.cause.raise
{EXCEPTION_MANAGER}.last_exception.raise
The first one goes through the chain of exceptions ignoring those triggered as a result of failed routines. It might be the one you are looking for.
The next two retrieve either the cause of the current exception or the current exception itself, though it might be not the one you are looking for because it depends on the context of the exception and the nested calls.

eclipselink hangs on bind

My EclipseLink JPA (Version 2.6.4) keeps hanging on bind every once in a while on a straight forward select * from entity call.
The OS is an Ubuntu Linux running a mySQL database. The OS, JPA and mySQL version has changed a few times over the last 3 years but the error still happens more often than not. The software currently runs in a Tomcat container.
Here is an excerpt from the code:
String sql="SELECT e FROM " + entityName +" e";
LOGGER.log(Level.INFO,sql+"["+this.getPuName()+"]");
EntityManager em = getEntityManager();
Query query = em.createQuery(sql,this.getEntityType());
query.setMaxResults(maxResults);
result = query.getResultList();
In the log this leads to:
INFORMATION: SELECT e FROM Person e[com.bitplan.smartcrmRest]
[EL Fine]: sql: 2017-08-28 09:57:55.472--ServerSession(509447780)--Connection(1675227444)--Thread(Thread[http-bio-8090-exec-29,5,main])--SELECT PersonNummer AS a1, EMailStatus AS a2, OEGruppe AS a3, PLZPrivat AS a4, AdressStatus AS a5, akademischerTitel AS a6, aktuelleAuswahl AS a7, Anrede AS a8, Art AS a9, card AS a10, createdAt AS a11, DatenHerkunft AS a12, deletedAt AS a13, email AS a14, EntscheidungsFunktion AS a15, ErstelltVon AS a16, FaxPrivat AS a17, Geburtstag AS a18, Gruppe AS a19, Kommentar AS a20, lastModified AS a21, mailinglists AS a22, Mobiltelefon AS a23, Name AS a24, OrganisationsEinheit AS a25, OrtPrivat AS a26, otheremail AS a27, perDu AS a28, photo AS a29, PositionGruppe AS a30, Rolle AS a31, Sachbearbeiter AS a32, salesEstimate AS a33, salesRank AS a34, sex AS a35, socialLinks AS a36, Sprache AS a37, StrassePrivat AS a38, subid AS a39, TelPrivat AS a40, Telefax AS a41, Telefon AS a42, Titel AS a43, Verhaeltnis AS a44, Vorname AS a45, Wichtigkeit AS a46, MEINEORGANISATION_OrganisationNummer AS a47 FROM Person LIMIT ?, ?
bind => [0, 100000]
and then nothing happens. The behavior seems to depends on the amount of records being returned. We see this hang at some 50.000 records up regularly but it also happens with as few as 200 records. The hang is independent of the table being queried.
I have filed a bug report at https://bugs.eclipse.org/bugs/show_bug.cgi?id=521453
Others have experiences similar issues see e.g.
EclipseLink lazy loading hangs on reference class
This topic has been discussed earlier at
https://stackoverflow.com/questions/28297266/eclipselink-jpa-hangs-on-bind
and got some interest by the community.
Due to a mistake of mine there was a misunderstanding and the question has been deleted (see quite lively discussion at My question got deleted by a moderator; how can I find out the reason?).
This is a restart with the intention to also provide
a Minimal, Complete, and Verifiable example see https://stackoverflow.com/help/mcve
So my question would be - what is a good way to make this reproducible?
My first thought would be to provide a Docker container using JPA, mySQL and some dummy data and try to get the same issue. What else might be a good approach?
Addendum 2017-12-10: As a hint the following thread dump information might help.
Searching for the mentioned ConcurrencyManager I found e.g.
https://www.eclipse.org/forums/index.php/t/524575/
Thread dump
"http-bio-8090-exec-22" #46 daemon prio=5 os_prio=0 tid=0x00007f1fdc070800 nid=0x1287 waiting on condition [0x00007f1fe7dfa000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at org.eclipse.persistence.internal.helper.ConcurrencyManager.releaseDeferredLock(ConcurrencyManager.java:445)
at org.eclipse.persistence.internal.identitymaps.CacheKey.releaseDeferredLock(CacheKey.java:458)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:1069)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildWorkingCopyCloneNormally(ObjectBuilder.java:898)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObjectInUnitOfWork(ObjectBuilder.java:851)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:734)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:688)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.buildObject(ObjectLevelReadQuery.java:803)
at org.eclipse.persistence.queries.ReadAllQuery.registerResultInUnitOfWork(ReadAllQuery.java:897)
at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:516)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1174)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:899)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1133)
at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:403)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1221)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1802)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1784)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1749)
at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:258)
at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:469)
at com.bitplan.storage.sql.JPAEntityManager.findAll(JPAEntityManager.java:260)
at com.bitplan.resthelper.ManagerResourceImpl.getAll(ManagerResourceImpl.java:161)
at org.eclipse.persistence.internal.helper.ConcurrencyManager.acquire(ConcurrencyManager.java:80)
- locked <0x000000009d300db0> (a org.eclipse.persistence.internal.identitymaps.HardCacheWeakIdentityMap$ReferenceCacheKey)
at org.eclipse.persistence.internal.identitymaps.CacheKey.acquire(CacheKey.java:132)
at org.eclipse.persistence.internal.identitymaps.AbstractIdentityMap.acquireLock(AbstractIdentityMap.java:108)
at org.eclipse.persistence.internal.identitymaps.IdentityMapManager.acquireLock(IdentityMapManager.java:157)
at org.eclipse.persistence.internal.sessions.IdentityMapAccessor.acquireLock(IdentityMapAccessor.java:99)
at org.eclipse.persistence.internal.sessions.IdentityMapAccessor.acquireLock(IdentityMapAccessor.java:90)
at org.eclipse.persistence.internal.sessions.AbstractSession.retrieveCacheKey(AbstractSession.java:5157)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:964)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:736)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:688)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.buildObject(ObjectLevelReadQuery.java:803)
at org.eclipse.persistence.queries.ReadObjectQuery.executeObjectLevelReadQuery(ReadObjectQuery.java:554)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1174)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:899)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1133)
at org.eclipse.persistence.queries.ReadObjectQuery.execute(ReadObjectQuery.java:431)
at org.eclipse.persistence.internal.sessions.AbstractSession.internalExecuteQuery(AbstractSession.java:3212)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1802)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1784)
at org.eclipse.persistence.internal.indirection.NoIndirectionPolicy.valueFromQuery(NoIndirectionPolicy.java:326)
at org.eclipse.persistence.mappings.ForeignReferenceMapping.valueFromRowInternal(ForeignReferenceMapping.java:2328)
at org.eclipse.persistence.mappings.OneToOneMapping.valueFromRowInternal(OneToOneMapping.java:1848)
at org.eclipse.persistence.mappings.ForeignReferenceMapping.valueFromRow(ForeignReferenceMapping.java:2178)
at org.eclipse.persistence.mappings.ForeignReferenceMapping.readFromRowIntoObject(ForeignReferenceMapping.java:1505)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildAttributesIntoObject(ObjectBuilder.java:461)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:1004)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildWorkingCopyCloneNormally(ObjectBuilder.java:898)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObjectInUnitOfWork(ObjectBuilder.java:851)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:734)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:688)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.buildObject(ObjectLevelReadQuery.java:803)
at org.eclipse.persistence.queries.ReadAllQuery.registerResultInUnitOfWork(ReadAllQuery.java:897)
at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:516)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1174)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:899)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1133)
at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:403)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1221)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1802)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1784)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1749)
at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:258)
at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:469)
at com.bitplan.storage.sql.JPAEntityManager.findAll(JPAEntityManager.java:260)
at com.bitplan.resthelper.ManagerResourceImpl.getAll(ManagerResourceImpl.java:161)
In the meantime I suspect that
ConcurrencyManager.releaseDeferredLock
is the culprit.
see e.g.
https://bugs.eclipse.org/bugs/show_bug.cgi?id=389227
https://bugs.eclipse.org/bugs/show_bug.cgi?id=464105
https://bugs.eclipse.org/bugs/show_bug.cgi?id=440085

Rails where query with dates does not return boundary date data

I am trying to retrieve data from mysql database using rails. it is working fine but it does not return the data which are near to boundary dates. for example
if params[:from] and params[:to]
fr = nil;
fr = DateTime.parse(params[:from]) unless params[:from].empty?
to = nil
to = DateTime.parse(params[:to]) unless params[:to].empty?
if !fr.nil? and !to.nil?
puts "------------------"
puts fr
puts to
#m = #m.where(start: fr..to)
end
end
#m.size
Logs info
App 30303 stdout: ------------------
App 30303 stdout:
App 30303 stdout: 2015-04-12T16:26:16+02:00
App 30303 stdout:
App 30303 stdout: 2015-04-13T16:26:16+02:00
App 30303 stdout:
App 30303 stdout: 0
I am sure that there is data for and hour before
268129 | 2015-04-13 15:21:53 | 2015-04-13 15:21:53 |
where first is id, second column is start date
So for sure date are correct as I can see in logs and particular user has data also. I am trying to extract data from last 24 hours.
Any suggestions?
ok so the problem is that the code uses a time zone while the database seems to hold a datetime object without timezone info, so we need to tell rails to take that time and assume it's normal utc time, hopefully this works.
if params[:from] && params[:to]
Time.use_zone('UTC') do
fr = Time.parse params[:from]
to = Time.parse params[:to]
end
#m = #m.where(start: fr..to)
end
end
#m.size

Error: bad argument #1 to 'insert' (table expected, got nil)

I am trying to connect to a mysql server using LuaSql via a mysql proxy. I try to execute a simple program (db.lua):
require("luasql.mysql")
local _sqlEnv = assert(luasql.mysql())
local _con = nil
function read_auth(auth)
local host, port = string.match(proxy.backends[1].address, "(.*):(.*)")
_con = assert(_sqlEnv:connect( "db_name", "username", "password", "hostname", "3306"))
end
function disconnect_client()
assert(_con:close())
end
function read_query(packet)
local cur = con:execute("select * from t1")
myTable = {}
row = cur:fetch(myTable, "a")
print(myTable.id,myTable.user)
end
This code executes well when I execute it without mysql-proxy. When I am connecting with mysql-proxy, the error-log displays these errors:
mysql.lua:8: bad argument #1 to 'insert' (table expected, got nil)
db.lua:1: loop or previous error loading module 'luasql.mysql'
mysql.lua is a default file of LuaSql:
---------------------------------------------------------------------
-- MySQL specific tests and configurations.
-- $Id: mysql.lua,v 1.4 2006/01/25 20:28:30 tomas Exp $
---------------------------------------------------------------------
QUERYING_STRING_TYPE_NAME = "binary(65535)"
table.insert (CUR_METHODS, "numrows")
table.insert (EXTENSIONS, numrows)
---------------------------------------------------------------------
-- Build SQL command to create the test table.
---------------------------------------------------------------------
local _define_table = define_table
function define_table (n)
return _define_table(n) .. " TYPE = InnoDB;"
end
---------------------------------------------------------------------
-- MySQL versions 4.0.x do not implement rollback.
---------------------------------------------------------------------
local _rollback = rollback
function rollback ()
if luasql._MYSQLVERSION and string.sub(luasql._MYSQLVERSION, 1, 3) == "4.0" then
io.write("skipping rollback test (mysql version 4.0.x)")
return
else
_rollback ()
end
end
As stated in my previous comment, the error indicates that table.insert (CUR_METHODS, ...) is getting a nil as first arg. Since the first arg is CUR_METHODS, it means that this object CUR_METHODS has not been defined yet. Since this happens near top of the luasql.mysql module, my guess is that the luasql initialization was incomplete, maybe because the mysql DLL was not found. My guess is that the LUA_CPATH does not find the MySQL DLL for luasql, but I'm surprised that you wouldn't get a package error, so something odd is going on. You'll have to dig into the luasql module and C file to figure out why it is not being created.
Update: alternately, update your post to show the output of print("LUA path:", package.path) and print("LUA path:", package.cpath) from your mysql-proxy script and also show the path of folder where luasql is installed and contents of that folder.

How to log error queries in mysql?

I know that there is general_log that logs all queries, but I want to find out which query has an error, and get the error message. I have tried running an error query on purpose, but it logs as a normal query and doesn't report it with error. Any ideas?
There is no functionality in MySQL to do this.
You will have to stick with application-layer logging.
I know this is ancient now, but for anyone having the same issue that lands here from Google, here's my two cents.
If you're using the cli client, you can simply redirect your sterr to a file, and then parse through that.
mysql -u user -p 2> errors.log
It is possible to log error queries with the MariaDB Audit Plugin.
The MariaDB Audit Plugin works for MariaDB, MySQL and Percona Server.
For example, for these queries
select now();
select now()+();
select 9+();
select 'hello';
log seems like this:
20150807 23:00:36,mnv-Satellite-L300D,root,localhost,82,377,QUERY,`test`,'select now()
LIMIT 0, 1000',0
20150807 23:00:37,mnv-Satellite-L300D,root,localhost,82,379,QUERY,`test`,'select now()+()',1064
20150807 23:00:37,mnv-Satellite-L300D,root,localhost,82,382,QUERY,`test`,'select 9+()',1064
20150807 23:00:38,mnv-Satellite-L300D,root,localhost,82,383,QUERY,`test`,'select \'hello\'
LIMIT 0, 1000',0
Last column is return code. 0 is Ok. Else - error.
Even though this question is quite old I hope it will be useful to someone who searched for mysql log error queries or similar terms.
Not too long ago I also required mysqld to log only erroneous queries. I found that mysql-proxy enables you to do that and wrote a small LUA script:
local err_flag = false
function read_query( packet )
if packet:byte() == proxy.COM_QUERY then
local user = proxy.connection.client.username
local host = proxy.connection.client.src.name
if user:lower() == 'someuser' then -- change this to any condition where errors should be logged
proxy.queries:append(1, packet, {resultset_is_needed = true})
proxy.queries:append(2, string.char(proxy.COM_QUERY) .. "SET #last_query = '" .. string.sub(packet, 2) .. "'", {resultset_is_needed = true} )
proxy.queries:append(3, string.char(proxy.COM_QUERY) .. "SHOW WARNINGS", {resultset_is_needed = true} )
end
return proxy.PROXY_SEND_QUERY
end
end
function insert_query(err_t, err_n, err_m)
local query = "INSERT INTO `somedb`.`mysql_error` " .. -- change log destination
"(`date`, `err_num`,`err_type`, `err_message`, `problem_query`, `conn_id`)" ..
" VALUES ( NOW(), " ..
err_n .. "," .. "\"" ..
err_t .."\"" .. "," .. "\"" ..
err_m .. "\"" .. "," ..
"#last_query" .. "," ..
proxy.connection.server.thread_id .. ")"
proxy.queries:append(4, string.char(proxy.COM_QUERY) .. query, {resultset_is_needed = true})
return proxy.PROXY_SEND_QUERY
end
function read_query_result(inj)
local res = assert(inj.resultset)
if inj.id == 1 then
err_flag = false
if res.query_status == proxy.MYSQLD_PACKET_ERR then
err_flag = true
return proxy.PROXY_IGNORE_RESULT
end
elseif inj.id == 2 then
return proxy.PROXY_IGNORE_RESULT
elseif inj.id == 3 then
if err_flag == true then
for row in res.rows do
proxy.response.type = proxy.MYSQLD_PACKET_ERR
proxy.response.errmsg = row[3]
insert_query(row[1], row[2], row[3])
end
return proxy.PROXY_SEND_RESULT
end
return proxy.PROXY_IGNORE_RESULT
elseif inj.id == 4 then
return proxy.PROXY_IGNORE_RESULT
end
end
DDL needed for logging table, adjust somedb.mysql_error to liking, but don't forget to do so in the above LUA script also.
CREATE TABLE `somedb`.`mysql_error` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` datetime NOT NULL,
`err_num` smallint(6) NOT NULL,
`err_type` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`err_message` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`problem_query` varchar(8000) COLLATE utf8_unicode_ci NOT NULL,
`conn_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
To use the script, run
/path/to/mysql-proxy --proxy-lua-script=/path/to/mysql-proxy-log-error-queries.lua
or if that fails (>=v0.9)
/path/to/mysql-proxy --proxy-lua-script=/path/to/mysql-proxy-log-error-queries.lua --plugins=proxy
The proxy runs on port 4040 by default, to test:
mysql -u username -p --host=127.0.0.1 --port=4040
and run some faulty sql.
When everything seems to be in order set the port in your application to 4040 instead of the actual mysqld port and you have mysql error logging on the database level.
Final note: mysql-proxy is beta. Use with caution I guess. Been running here for almost half a year now without problems, however YMMV.
I have tried running an error query on
purpose, but it logs as a normal query
and doesn't report it with error. Any
ideas?
so, you did it wrong. No other idea without the code.
in PHP I'm doing it this way (assume you're using mysql driver):
$res=mysql_query($sql) or trigger_error(mysql_error().$sql);
it will log all erroneous queries if you have log_errors setting on (and you have to)
EDIT:
I see now, you want global level logging,m not application level.
But may be application level will suit you as well?
MariaDB is capable to do this with a plugin https://mariadb.com/kb/en/sql-error-log-plugin/ which is distributed with mariadb.
I tried this yesterday and it works as advertised.
One need to just run:
install plugin SQL_ERROR_LOG soname 'sql_errlog';
and the queries with errors will go to $datadir/sql_errors.log that is on most linux install at /var/lib/mysql/sql_errors.log.